Overview:
As we know that time is the most important concern in our life. To do any task easily, We have to use software to perform it in a simpler manner. Software is a set of programs with related documents. The Software Program or Program is a set of instructions to do a specific task. Here, our specific task is to reverse a string. First, I want to explain What is String? Then, I will explain What do you mean by Reverse? Moreover, I will explain What is a palindrome? Furthermore, I will introduce you to What is a Program? After all, I will write the logic of a C program to reverse a string and explain it with output.
Table of contents:
- What is a String?
- What do you mean by Reverse?
- What is a palindrome?
- What is a Program?
- Demonstration of Reverse a string.
- C program to reverse a string by using C Library function strrev()
- C program to reverse a string by using Array
- Explanation of the C program to reverse a string with output.
What is a String?
The String is a combination of characters that may be any digit from 0 to 9 and Alphabets from A to Z. The repetition of characters does not matter. For instance, a String Conax uses 5 characters i.e. C,o,n,e,and x.
What do you mean by Reverse?
Reverse means move backward and turned toward the direction opposite to that previously stated. For instance, If our string is Conax then its reverse string is Xanoc.
What is a palindrome?
A Palindrome is an approach in which the reversed string is the same as the previous string. For instance, the Reverse of a string 10101 is also 10101.
What is a Program?
A Program is a set of instructions to perform a specific task. A Program tells the Computer What to do? And a Logic tells the computer How to do it? To Write a Program We will construct our own logic related to the task.
Demonstration of Reverse a string.
- Step 1: Take a String
- Step 1: Move backward the last character to the previous string.
- Step 2: Turned toward the direction opposite to it. That became the last character to the previous one as so on.
- Step 3: At last Write the continuous process as a string until the last characters as a sequence.
Example: Demonstration of Reverse a string Conax.
C program to reverse a string by using C Library function strrev()
#include <stdio.h> #include <string.h> int main() { char s[100]; printf("Please! Enter a string that you want to reverse: "); gets(s); strrev(s); printf("The Reverse of the entered string is: %s", s); return 0; }
Save this program with the filename and extension .c like filename.c. Then compile it on turbo C or another compiler.
The output of the Program with code:
Please! Enter a string that you want to reverse: Conax
The Reverse of the entered string is: xanoC
C program to reverse a string by using Array
#include <stdio.h> int main() { char s[1000], r[1000]; int start, last, count = 0; printf("Please! Input a string: "); gets(s); // Calculating string length while (s[count] != '\0') count++; last = count - 1; for (start = 0; start < count; start++) { r[start] = s[last]; last--; } r[start] = '\0'; printf("The Reversed String is:%s\n", r); return 0; }
The output of the Program with code:
Please! Enter a string that you want to reverse: Conax
The Reverse of the entered string is: xanoC
Conclusion: Explanation of the C program to reverse a string with output.
The internal working of this program depends upon the previous demonstration of Reverse a string.
After the successful compilation of the program, a message is displayed on the screen as Please! Enter a string that you want to reverse: Suppose, User, entered Conax then again a message is displayed as Reverse of the entered string is: xanoC.