Reverse a String using Loops in C
In C, we can reverse a string using different looping techniques such as the for
loop and the while
loop. Reversing a string involves iterating through the characters from the end to the beginning and storing them in a new string or swapping characters in-place.
Examples to Reverse a String Using Loops
1. Reverse a String Using a for
Loop
In this example, we will reverse a string by iterating from the last character to the first and storing it in a new string.
main.c
#include <stdio.h>
#include <string.h>
int main() {
char str[] = "Hello";
int len = strlen(str);
char rev[len + 1];
// Using for loop to reverse the string
for (int i = 0; i < len; i++) {
rev[i] = str[len - i - 1];
}
rev[len] = '\0'; // Null terminator
printf("Reversed String: %s\n", rev);
return 0;
}
Explanation:
- We declare the original string
str[]
and determine its length usingstrlen()
. - We create an empty array
rev[]
to store the reversed string. - Using a
for
loop, we iterate from the last character ofstr
to the first and copy each character torev[]
. - We add a null terminator
'\0'
at the end to mark it as a valid string. - Finally, we print the reversed string using
printf()
.
Output:
Reversed String: olleH
2. Reverse a String In-Place Using a while
Loop
In this example, we will reverse a string in-place by swapping characters from both ends using a while
loop.
main.c
#include <stdio.h>
#include <string.h>
void reverseString(char str[]) {
int left = 0, right = strlen(str) - 1;
char temp;
// Using while loop to swap characters
while (left < right) {
temp = str[left];
str[left] = str[right];
str[right] = temp;
left++;
right--;
}
}
int main() {
char str[] = "World";
reverseString(str);
printf("Reversed String: %s\n", str);
return 0;
}
Explanation:
- We define a function
reverseString()
that takes a character array as input. - We initialize two indices:
left
starting at the first character andright
starting at the last character. - Inside a
while
loop, we swap the characters atleft
andright
and then move them towards the center. - The process continues until
left
is no longer less thanright
. - Since we modify the string in-place, we directly print the modified
str
in themain()
function.
Output:
Reversed String: dlroW
3. Reverse a String Using Recursion
In this example, we will reverse a string using recursion, which repeatedly swaps characters until the entire string is reversed.
main.c
#include <stdio.h>
#include <string.h>
void reverseRecursive(char str[], int left, int right) {
if (left >= right) return;
// Swap characters
char temp = str[left];
str[left] = str[right];
str[right] = temp;
// Recursive call
reverseRecursive(str, left + 1, right - 1);
}
int main() {
char str[] = "Recursion";
reverseRecursive(str, 0, strlen(str) - 1);
printf("Reversed String: %s\n", str);
return 0;
}
Explanation:
- We define a recursive function
reverseRecursive()
that swaps characters from both ends. - We use a base case: if
left
is greater than or equal toright
, we stop recursion. - We swap characters at positions
left
andright
. - We call
reverseRecursive()
again, movingleft
forward andright
backward. - The function continues until the entire string is reversed, then the modified string is printed.
Output:
Reversed String: noisruceR
Conclusion
In this tutorial, we explored different ways to reverse a string in C:
- Using a
for
loop – Copying characters in reverse order. - Using a
while
loop – Swapping characters in-place. - Using recursion – Swapping characters using a function call stack.
Each method has its own advantages depending on the use case.