0
0
CppProgramBeginner · 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 reverseString(string &str, int start, int end) that swaps characters at start and end and calls itself with updated indices until they meet or cross.
📋

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 is greater than or equal to the end index, which means the string is fully reversed.
📐

Algorithm

1
Take the string and two indices: start at 0 and end at string length minus one.
2
If start is greater than or equal to end, stop the recursion.
3
Swap the characters at start and end positions.
4
Call the function again with start increased by one and end decreased by one.
5
Repeat until the entire string is reversed.
💻

Code

cpp
#include <iostream>
#include <string>
using namespace std;

void reverseString(string &str, int start, int end) {
    if (start >= end) return;
    swap(str[start], str[end]);
    reverseString(str, start + 1, end - 1);
}

int main() {
    string s = "hello";
    reverseString(s, 0, s.length() - 1);
    cout << s << endl;
    return 0;
}
Output
olleh
🔍

Dry Run

Let's trace the string "hello" through the recursive reverse function.

1

Initial call

reverseString("hello", 0, 4) swaps 'h' and 'o' -> "oellh"

2

Second call

reverseString("oellh", 1, 3) swaps 'e' and 'l' -> "olleh"

3

Third call

reverseString("olleh", 2, 2) start equals end, recursion stops

CallStart IndexEnd IndexString State
104oellh
213olleh
322olleh
💡

Why This Works

Step 1: Base Case

The recursion stops when the start index is greater than or equal to the end index, meaning the string is fully reversed or has one character left.

Step 2: Swapping Characters

Each recursive call swaps the characters at the current start and end positions to move characters to their reversed positions.

Step 3: Recursive Call

The function calls itself with updated indices moving inward, gradually reversing the entire string.

🔄

Alternative Approaches

Using a helper function that returns a new reversed string
cpp
#include <iostream>
#include <string>
using namespace std;

string reverseString(const string &str) {
    if (str.empty()) return "";
    return reverseString(str.substr(1)) + str[0];
}

int main() {
    string s = "hello";
    cout << reverseString(s) << endl;
    return 0;
}
This method creates new strings at each call, which is simpler but uses more memory.
Using iteration instead of recursion
cpp
#include <iostream>
#include <string>
using namespace std;

void reverseString(string &str) {
    int n = str.length();
    for (int i = 0; i < n / 2; i++) {
        swap(str[i], str[n - i - 1]);
    }
}

int main() {
    string s = "hello";
    reverseString(s);
    cout << s << endl;
    return 0;
}
Iterative approach is usually faster and uses less stack memory than recursion.

Complexity: O(n) time, O(n) space

Time Complexity

The function swaps pairs of characters moving inward, so it runs in linear time proportional to the string length.

Space Complexity

Recursion uses stack space proportional to half the string length, so space complexity is O(n).

Which Approach is Fastest?

The iterative approach is fastest and uses constant space, while recursive methods use extra stack space.

ApproachTimeSpaceBest For
Recursive in-place swapO(n)O(n)Learning recursion and in-place reversal
Recursive new stringO(n^2)O(n^2)Simple code but inefficient for large strings
Iterative swapO(n)O(1)Best for performance and memory efficiency
💡
Always define a clear base case in recursion to avoid infinite calls.
⚠️
Forgetting to update the start and end indices in the recursive call causes infinite recursion.