How do you reverse a string in place in C or C++?



Reversing a string means, moving first character to last, second character to second last and so on. In this article we will discuss how to reverse a string in place using C/C++ program. In the "in place" string reversing, we are not allowed take extra memory to store the string while running program. First of all, let's understand our problem statement:

We are given a character string as input and we need to find and output the reverse string of the given string. For example:
// Input : a character string
"This is a string"

//Output : Reverse of the string
"gnirts a si sihT" 

Reversing a String in Place

Here is the list of approaches to reverse a string in place using c/c++ program, which we will be discussing in this article with stepwise explanation and complete example codes.

Using Recursion to Reverse a String

This method uses a recursion technique to swap characters of string to reverse the string. This is not recommended for larger strings, as it can cause stack overflow error. Now, let's see how to implement this method using an example.

Example

In the code below we defined a recursion function that swaps left most and right most characters at a time and then move to next stage with left + 1 and right - 1. This process will continue untill left become greater than or equal to right, which means the string is reversed.

#include <iostream>
using namespace std;

void reverseRecursive(string& str, int left, int right) {
    if (left >= right) return;
    swap(str[left], str[right]);
    reverseRecursive(str, left + 1, right - 1);
}

int main() {
    string str = "reverse";
    reverseRecursive(str, 0, str.length() - 1);
    cout << str;  // Output: "esrever"
}

The output of the above code will be:

esrever

Using Two Pointer Technique to Reverse a String

In two pointer technique, we will run two pointers from first and last of the string, and swap each characters one by one from first and last. Compared to other methods, this is very fast and also uses less dependencies. Now let's see how to implement this method to a string.

Example

The code below runs two pointers from first and last of string using a while loop. At each index, it swaps the left and right characters using swap() function.

#include <iostream>
using namespace std;

int main() {
    string str = "world";
    int left = 0, right = str.length() - 1;

    while (left < right) {
        swap(str[left], str[right]);
        ++left;
        --right;
    }

    cout << str;  // Output: "dlrow"
}

The output of the above code will be:

dlrow

Using reverse() Function to Reverse a String

The std:reverse() function in the algorithm header is used to find reverse of vectors and strings. It modifies the original sequence directly, without creating a copy. The function takes two iterators as arguments, defining the beginning and end of the range to be reversed. Now, let's see how to implement this function to reverse a string

Example

In the code below, the reverse() function takes iterators at first and end of the string, then return the reverse of string with taking an extra space.

#include <iostream>
#include <algorithm>
using namespace std;

int main() {
    string str = "hello";
    reverse(str.begin(), str.end());
    cout << str;  // Output: "olleh"
}

The output of the above code will be:

olleh
Farhan Muhamed
Farhan Muhamed

No Code Developer, Vibe Coder

Updated on: 2025-04-22T11:42:12+05:30

646 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements