C program to find the reverse of a string – In this article, we will discuss the several methods used to find the reverse of a string in C programming.
Suitable examples and sample programs have also been added so that you can understand the whole thing very clearly. The compiler has also been added with which you can execute it yourself.
The methods used in this piece are as follows:
- Using Standard Method
- Using Function
- Using Recursion
- Using String Library Function
A string is nothing but an array of characters. The value of a string is determined by the terminating character. Its value is considered to be 0.
As you can see, you need to enter a string of your own choice firstly.
The string entered here is a simple “WELCOME”.
The string that will emerge after it is reversed is this:
EMOCLEW
Thus, the means to do so in C programming are as follows:
Using Standard Method
- Read the entered string and store the string into the variable s.
2) Calculate the string length using strlen(String s) and store into the variable n.
3) Iterate the for loop from i=0 to i<half of the length of the string.
a) Initialize the starting element to temp.
b) Initialize the s[n-i-1] element to s[i].Here last positioned character initialized to the first position.
c) Initialize the temp to last position.
when ever i value increased then the position will be decreased.
4) Print the reverse of a string.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
#include <stdio.h> #include <string.h> int main() { char s[1000]; int i,n,temp; printf("Enter the string : "); gets(s); n=strlen(s); printf("before reverse = %s\n",s); for(i=0;i<n/2;i++) { temp=s[i]; s[i]=s[n-i-1]; s[n-i-1]=temp; } printf("after reverse = %s\n",s); return 0; } |
Output:
1 2 3 |
Enter the string: WELCOME before reverse = WELCOME after reverse = EMOCLEW |
Using Function
- The main() calls the stringreverse(char *s) function by passing the string as an argument to that function.
2) The function first calculates the string length using string library function strlen(String) and assign to n.
a) Iterate the for loop from i=0 to i<half of the length of the string.
Initialize the starting element s[i] to temp.
Initialize the character from the last position s[n-i-1] to starting position s[i].
Initialize the temp to last position s[n-i-1].
3) Print the reverse string.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
#include <stdio.h> #include <string.h> void stringreverse(char *s) { int i,temp,n; n=strlen(s); for(i=0;i<n/2;i++) { temp=s[i]; s[i]=s[n-i-1]; s[n-i-1]=temp; } } int main() { char s[1000]; printf("Enter the string: "); gets(s); printf("before reverse = %s\n",s); stringreverse(s); printf("after reverse = %s\n",s); } |
Output:
1 2 3 |
Enter the string: hello world before reverse = hello world after reverse = dlrow olleh |
Using Recursion
- The function stringreverse(char *s) calculates the string length using strlen(s) and length will be stored in the variable n.
a) If i<length of the string/2
initialize the s[i] to temp
initialize element from last index n-i-1 to s[i] and temp to last index s[n-i-1].
i value increased by 1
the function calls itself recursively until i< half of the string length.
2)Print the reverse string.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
#include <stdio.h> #include <string.h> void stringreverse(char *s) { static int i,temp,n=strlen(s); if(i<n/2) { temp=s[i]; s[i]=s[n-i-1]; s[n-i-1]=temp; i++; stringreverse(s); } } int main() { char s[1000]; printf("Enter the string: "); gets(s); printf("before reverse = %s\n",s); stringreverse(s); printf("after reverse = %s\n",s); } |
1 2 3 |
Enter the string: Inspirational before reverse = Inspirational after reverse = lanoitaripsnI |
Using String Library Function
- The function strrev(String s) is the one of the library function of string. It will reverse the given string.
2) The function strrev(String s) is available in the string.h header file.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
#include <stdio.h> #include <string.h> int main() { char s[1000]; printf("Enter the string: "); gets(s); printf("before reverse = %s\n",s); strrev(s); printf("after reverse = %s\n",s); return 0; } |
1 2 3 |
Enter the string: Cbeginners before reverse = Cbeginners after reverse = srennigebC |