C Program to Reverse String Using Recursion
void reverse(char *str, int start, int end) that swaps characters at start and end and calls itself with start+1 and end-1 until start >= end.Examples
How to Think About It
Algorithm
Code
#include <stdio.h> #include <string.h> void reverse(char *str, int start, int end) { if (start >= end) return; char temp = str[start]; str[start] = str[end]; str[end] = temp; reverse(str, start + 1, end - 1); } int main() { char str[100]; printf("Enter a string: "); fgets(str, sizeof(str), stdin); int len = strlen(str); if (len > 0 && str[len - 1] == '\n') str[len - 1] = '\0'; reverse(str, 0, strlen(str) - 1); printf("Reversed string: %s\n", str); return 0; }
Dry Run
Let's trace the string "abc" through the recursive reverse function.
Initial call
reverse("abc", 0, 2) swaps 'a' and 'c' -> string becomes "cba"
Recursive call
reverse("cba", 1, 1) since start == end, recursion stops
| Call | start | end | String state |
|---|---|---|---|
| 1 | 0 | 2 | abc -> cba |
| 2 | 1 | 1 | cba (no change) |
Why This Works
Step 1: Base case stops recursion
When start >= end, the function stops calling itself to avoid infinite loops.
Step 2: Swapping characters
Swapping characters at start and end moves the first and last characters into reversed positions.
Step 3: Recursive call shrinks problem
Calling the function with start+1 and end-1 works on the smaller substring inside, repeating the process.
Alternative Approaches
#include <stdio.h> #include <string.h> void reverse_iterative(char *str) { int start = 0, end = strlen(str) - 1; while (start < end) { char temp = str[start]; str[start] = str[end]; str[end] = temp; start++; end--; } } int main() { char str[100]; printf("Enter a string: "); fgets(str, sizeof(str), stdin); int len = strlen(str); if (len > 0 && str[len - 1] == '\n') str[len - 1] = '\0'; reverse_iterative(str); printf("Reversed string: %s\n", str); return 0; }
#include <stdio.h> #include <string.h> void reverse_helper(char *src, char *dest, int index) { if (index < 0) { dest[strlen(src)] = '\0'; return; } dest[strlen(src) - 1 - index] = src[index]; reverse_helper(src, dest, index - 1); } int main() { char str[100], rev[100]; printf("Enter a string: "); fgets(str, sizeof(str), stdin); int len = strlen(str); if (len > 0 && str[len - 1] == '\n') str[len - 1] = '\0'; reverse_helper(str, rev, strlen(str) - 1); printf("Reversed string: %s\n", rev); return 0; }
Complexity: O(n) time, O(n) space
Time Complexity
The function swaps characters from both ends moving inward, so it runs in linear time proportional to the string length, O(n).
Space Complexity
Recursion uses stack space for each call, so space complexity is O(n) due to call stack depth equal to half the string length.
Which Approach is Fastest?
The iterative method is faster in practice because it avoids recursive call overhead and uses O(1) space, but recursion is elegant and easy to understand.
| Approach | Time | Space | Best For |
|---|---|---|---|
| Recursive in-place swap | O(n) | O(n) | Learning recursion and elegant code |
| Iterative two-pointer swap | O(n) | O(1) | Performance and memory efficiency |
| Recursive with new string | O(n) | O(n) | Keeping original string unchanged |