0
0
CProgramBeginner · 2 min read

C Program to Reverse String Using Recursion

You can reverse a string in C using recursion by defining a function like 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

Inputhello
Outputolleh
Inputabc
Outputcba
Input
Output
🧠

How to Think About It

To reverse a string using recursion, think of swapping the first and last characters, then calling the same process on the smaller string inside those characters. Keep doing this until the start index meets or passes the end index, which means the string is fully reversed.
📐

Algorithm

1
Get the string and its start and end positions.
2
If start is greater than or equal to end, stop recursion.
3
Swap the characters at start and end positions.
4
Call the function recursively with start+1 and end-1.
5
When recursion ends, the string is reversed.
💻

Code

c
#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.

1

Initial call

reverse("abc", 0, 2) swaps 'a' and 'c' -> string becomes "cba"

2

Recursive call

reverse("cba", 1, 1) since start == end, recursion stops

CallstartendString state
102abc -> cba
211cba (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

Using two pointers iteratively
c
#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;
}
This method uses a loop instead of recursion, which can be easier to understand and avoids function call overhead.
Using a helper function to build reversed string
c
#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;
}
This approach creates a new reversed string instead of modifying the original, useful if you want to keep the original unchanged.

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.

ApproachTimeSpaceBest For
Recursive in-place swapO(n)O(n)Learning recursion and elegant code
Iterative two-pointer swapO(n)O(1)Performance and memory efficiency
Recursive with new stringO(n)O(n)Keeping original string unchanged
💡
Always check for the base case in recursion to prevent infinite calls.
⚠️
Forgetting to stop recursion when start index meets or passes end index causes infinite recursion.