Reverse String in C
Last Updated :
05 Mar, 2024
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
#include <stdio.h>
#include <string.h>
int main()
{
char str[100] = "string" ;
printf ( "Original String: %s\n" , str);
int len = strlen (str);
for ( int i = 0, j = len - 1; i <= j; i++, j--) {
char c = str[i];
str[i] = str[j];
str[j] = c;
}
printf ( "Reversed String: %s" , str);
return 0;
}
|
Output
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
#include <stdio.h>
#include <string.h>
void reverse( char * str, int len, int i, int temp)
{
if (i < len) {
temp = str[i];
str[i] = str[len - 1];
str[len - 1] = temp;
i++;
len--;
reverse(str, len, i, temp);
}
}
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;
}
|
Output
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
#include <stdio.h>
#include <string.h>
void stringReverse( char * str)
{
int len = strlen (str);
char * start = str;
char * end = str + len - 1;
while (start < end) {
char temp = *start;
*start = *end;
*end = temp;
start++;
end--;
}
}
int main()
{
char str[] = "string" ;
printf ( "Original String: %s\n" , str);
stringReverse(str);
printf ( "Reversed String: %s" , str);
return 0;
}
|
Output
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
#include <stdio.h>
#include <string.h>
int main()
{
char str[] = "string" ;
printf ( "Original String: %s\n" , str);
printf ( "Reversed String: %s" , strrev(str));
return 0;
}
|
Output
Original String: string
Reversed String: gnirts
Share your thoughts in the comments
Please Login to comment...