Reverse String in C
Reversing a string in C is a fundamental operation that involves rearranging the characters in a string so that the last character becomes the first, the second-to-last character becomes the second, and so on.
For example,
Original String:
"string"
Reversed String:
"gnirts"
In this article, we will discuss different ways to reverse a string in C with code examples.
Different Ways to Reverse a String in C
There are various ways to reverse the string in the C. Some of them are discussed below:
- Reverse the String Using Loop
- Reverse the String Using Recursion
- Reverse the String Using Pointer in C
- Reverse the String Using Library Function
1. Reverse the String Using Loop
In this method,
- We use a for loop with two variables i and j pointing to the start and end of the string respectively.
- The we replace the characters at indexes i and j, and move to the adjacent right and left respectively i.e. incrementing i and decrementing j.
- We keep doing that till i is greater than or equal to j.
We get the reversed string as the result.
Implementation
C
// C program to reverse the string in C using loops #include <stdio.h> #include <string.h> int main() { // string to be reversed. char str[100] = "string" ; printf ( "Original String: %s\n" , str); // string length int len = strlen (str); // for loop for ( int i = 0, j = len - 1; i <= j; i++, j--) { // swapping characters char c = str[i]; str[i] = str[j]; str[j] = c; } printf ( "Reversed String: %s" , str); return 0; } |
Original String: string Reversed String: gnirts
2. Reverse the String Using Recursion
For this method, we will use recursion to swap the characters.
Implementation
C
// C program to reverse string using recursion #include <stdio.h> #include <string.h> // recursive function to reverse string void reverse( char * str, int len, int i, int temp) { // if current index is less than the remaining length of // string if (i < len) { temp = str[i]; str[i] = str[len - 1]; str[len - 1] = temp; i++; len--; reverse(str, len, i, temp); } } // driver code int main() { char str[100] = "string" ; printf ( "Original String: %s\n" , str); int len = strlen (str); reverse(str, len, 0, 0); printf ( "Reversed String: %s" , str); return 0; } |
Original String: string Reversed String: gnirts
3. Reverse the String Using Pointer in C
We will use here two pointers, one is start pointer and another is end pointer. and by swapping the character we will proceed to achieve, reverse the characters similar to what we have done in the first method.
Implementation
C
// C program to reverse a string using pointers #include <stdio.h> #include <string.h> // function to reverse the string void stringReverse( char * str) { int len = strlen (str); // pointers to start and end char * start = str; char * end = str + len - 1; while (start < end) { char temp = *start; *start = *end; *end = temp; start++; end--; } } // driver code int main() { char str[] = "string" ; printf ( "Original String: %s\n" , str); // calling function stringReverse(str); printf ( "Reversed String: %s" , str); return 0; } |
Original String: string Reversed String: gnirts
4. Reverse the String Using Library Function
In C, we have a library function defined inside <string.h> that can be used to reverse a string. The strrev() function provides the simplest method to reverse the string.
Syntax
char* strrev(char* str);
where, str is the string to be reversed.
Note: The strrev() function is not a part of the standard C language, so it might not be present in every compiler.
Implementation
C
// C program to reverse a string using strrev() #include <stdio.h> #include <string.h> int main() { char str[] = "string" ; printf ( "Original String: %s\n" , str); // reversing string printf ( "Reversed String: %s" , strrev(str)); return 0; } |
Output
Original String: string
Reversed String: gnirts