Reverse a String in C
To reverse a string in C, we can use various approaches such as looping, recursion, and built-in functions. The most common ways include using a loop to swap characters, recursion to reverse the string in a function call stack, or using strrev()
(not part of standard C but available in some compilers).
In this tutorial, we will cover multiple methods to reverse a string with clear explanations and examples.
Examples of Reversing a String in C
1. Reversing a String Using a Loop
In this method, we will use a for
loop to swap characters from the beginning and end of the string until we reach the middle.
main.c
#include <stdio.h>
#include <string.h>
void reverseString(char str[]) {
int length = strlen(str);
int start = 0, end = length - 1;
char temp;
while (start < end) {
temp = str[start];
str[start] = str[end];
str[end] = temp;
start++;
end--;
}
}
int main() {
char str[] = "Hello";
reverseString(str);
printf("Reversed string: %s\n", str);
return 0;
}
Explanation:
- We include
string.h
for thestrlen()
function. - The function
reverseString()
takes a character array (str
) as input. - We find the length of the string using
strlen()
. - We initialize
start
to 0 andend
tolength - 1
. - Using a
while
loop, we swap characters from the start and end until the middle is reached. - The modified string is printed in the
main()
function.
Output:
Reversed string: olleH
2. Reversing a String Using Recursion
In this method, we use recursion to reverse the string by swapping characters and calling the function with a smaller substring.
main.c
#include <stdio.h>
#include <string.h>
void reverseRecursive(char str[], int start, int end) {
if (start >= end) return;
char temp = str[start];
str[start] = str[end];
str[end] = temp;
reverseRecursive(str, start + 1, end - 1);
}
int main() {
char str[] = "World";
reverseRecursive(str, 0, strlen(str) - 1);
printf("Reversed string: %s\n", str);
return 0;
}
Explanation:
- The function
reverseRecursive()
takes three parameters:str
,start
, andend
. - If
start
is greater than or equal toend
, we return (base case for recursion). - We swap the characters at
start
andend
. - We recursively call
reverseRecursive()
withstart + 1
andend - 1
. - The reversed string is printed in the
main()
function.
Output:
Reversed string: dlroW
3. Reversing a String Using strrev()
Some compilers provide the non-standard function strrev()
to reverse a string in place.
main.c
#include <stdio.h>
#include <string.h>
int main() {
char str[] = "Programming";
printf("Reversed string: %s\n", strrev(str));
return 0;
}
Explanation:
- We include
string.h
for thestrrev()
function. - The function
strrev(str)
reverses the string in place. - We print the reversed string using
printf()
.
Output:
Reversed string: gnimmargorP
Conclusion
In this tutorial, we explored different ways to reverse a string in C:
- Loop Method: Swaps characters from both ends of the string.
- Recursion: Calls itself to swap characters until the middle is reached.
strrev()
Function: Uses a built-in function (not standard C).
Each method has its advantages. Loops are efficient, recursion is useful for understanding call stacks, and strrev()
is the simplest if available in your compiler.