In this C++ tutorial, you will learn how to reverse a std::string using std::reverse(), a manual for loop with character swapping, right-to-left copying, and recursion. The examples also explain which methods modify the original string, their time and space costs, and a common character-array mistake to avoid.

Ways to reverse a string in C++

For a normal std::string, the simplest approach is usually std::reverse() from the <algorithm> header. If you are learning how string reversal works internally, you can instead swap characters from the two ends toward the center. You can also build a second string from right to left or use recursion.

MethodChanges original string?TimeExtra space
std::reverse()YesO(n)O(1)
Manual two-pointer swappingYesO(n)O(1)
Copy from right to leftNo, when written into a second stringO(n)O(n)
Recursion with swappingYesO(n)O(n) call stack

1. Reverse a C++ string using std::reverse()

std::reverse() is defined in the <algorithm> header. It reverses the elements in a range. For a std::string, pass str.begin() as the first iterator and str.end() as the iterator just past the last character.

</>
Copy
std::reverse(str.begin(), str.end());

The function reverses the string in place, so no second string is required. If str contains abcdef, the same variable contains fedcba after the call.

In the following example, we shall include algorithm header file and use reverse() function. Pass the beginning and ending of the string as arguments to reverse() function as shown in the program. This process reverses the string in-place.

C++ Program

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

int main() {
   string str = "And still, I rise.";
   reverse(str.begin(), str.end());
   cout << str << endl;
}

Output

.esir I ,llits dnA

Here, str.begin() points to the first character and str.end() marks the end of the range. The algorithm swaps characters from opposite ends until it reaches the middle. Its running time is linear in the string length. In self-contained C++ source files, also include <string> explicitly when using std::string.

2. Reverse a string in C++ without std::reverse() by swapping characters

If you want to reverse a string without using the library reversal function, swap the first character with the last, the second with the second-last, and continue until the loop reaches the middle. Only half of the string needs to be visited because each iteration places two characters in their final positions.

To reverse a string by swapping, you can follow the below steps.

  1. Start.
  2. Take string in variable str.
  3. Initialize variable index with 0.
  4. Check if index is less than half the length of str. If false, go to step 7.
  5. Swap str[index] with str[str length - 1 - index].
  6. Increment index, go to step 4.
  7. Stop.

In the following example, we shall implement the above steps and reverse the string.

C++ Program

</>
Copy
#include <iostream> 
using namespace std;

int main() {
   string str = "And still, I rise.";

   char ch;
   for (int index = 0, len = str.length(); index < len/2; index++) {
      ch = str[index];
      str[index] = str[len-1-index];
      str[len-1-index] = ch;
   }

   cout << str << endl;
}

Output

.esir I ,llits dnA

For a string of length len, the character opposite index index is at len - 1 - index. This method runs in O(n) time and uses O(1) extra space.

3. Reverse a C++ string by copying characters from right to left

Another approach is to leave the original string unchanged and copy its characters into a second sequence in reverse order. Start at the last character of the source string and write each character into the next position of the destination.

To reverse a string by copying from right to left, you can follow the below steps.

  1. Start.
  2. Take string in variable str.
  3. Take character array rev with size of str. This will hold the reversed string.
  4. Initialize variable index with 0.
  5. Check if index is less than the length of str. If false, go to step 8.
  6. Store str[str length - 1 - index] in rev[index].
  7. Increment index, go to step 4.
  8. Stop.

In the following example, we shall implement the above steps and reverse the string.

C++ Program

</>
Copy
#include <iostream> 
using namespace std;

int main() {
   string str = "And still, I rise.";
   char rev[str.length()];

   for (int index = 0, len = str.length(); (index < len); index++) {
      rev[index] = str[len-1-index];
   }

   cout << rev << endl;
}

Output

.esir I ,llits dnA

Portability note: the legacy character-array example above demonstrates the indexing idea, but the exact declaration char rev[str.length()] is a variable-length array, which is not part of standard C++. The array also needs a null terminator before it can safely be printed as a C-style string. For portable C++, prefer a second std::string as shown below.

</>
Copy
#include <iostream>
#include <string>
using namespace std;

int main() {
    string str = "And still, I rise.";
    string rev(str.length(), ' ');

    for (size_t index = 0; index < str.length(); index++) {
        rev[index] = str[str.length() - 1 - index];
    }

    cout << rev << endl;
}

This version keeps str unchanged, stores the result in rev, and uses O(n) additional space.

A shorter standard-library alternative is to construct a new string from reverse iterators. The range from str.rbegin() to str.rend() visits the source string from its last character to its first.

</>
Copy
string rev(str.rbegin(), str.rend());

4. Reverse a C++ string using recursion

A recursive solution can perform the same two-end swapping operation without an explicit loop. Each call swaps one pair of characters and then calls the function again with the left index moved forward and the right index moved backward.

In the following example, we shall write a recursive function that reverses a given string.

C++ Program

</>
Copy
#include <iostream>
using namespace std;

void reverseString(string& str, int n, int i) {
   if (n <= i) {
      return;
   }
   swap(str[i], str[n]);
   reverseString(str, n - 1, i + 1);
}

int main() {
   string str = "And still, I rise.";
   reverseString(str, str.length() - 1, 0);
   cout << str << endl;
}

Output

.esir I ,llits dnA

The base condition n <= i stops recursion when the two indices meet or cross. The reversal itself is O(n), but recursive calls use O(n) stack space, so this method is generally less space-efficient than an iterative swap.

Reverse a string while keeping the original C++ string unchanged

If you need both the original and reversed values, create a copy first and reverse the copy. This keeps the concise std::reverse() approach without losing the source string.

</>
Copy
#include <iostream>
#include <algorithm>
#include <string>
using namespace std;

int main() {
    string original = "TutorialKart";
    string reversed = original;

    reverse(reversed.begin(), reversed.end());

    cout << "Original: " << original << '\n';
    cout << "Reversed: " << reversed << '\n';
}

Output

Original: TutorialKart
Reversed: traKlairotuT

Choosing a C++ string reverse method

  • Use std::reverse(str.begin(), str.end()) when you simply need to reverse a mutable std::string in place.
  • Use the manual swapping loop when you want to learn or demonstrate how reversal works without the library function.
  • Build a second std::string when the original value must remain unchanged.
  • Use recursion mainly when recursion itself is the topic being practiced, because it consumes additional call-stack space.

All of these methods reverse the sequence of elements stored in the string. For ordinary single-byte text such as the examples above, this corresponds to reversing visible characters. Text containing multi-byte Unicode encodings may need Unicode-aware processing rather than byte or code-unit reversal.

C++ string reverse summary

In this C++ Tutorial, we learned how to reverse a string using std::reverse(), a manual for loop, right-to-left copying, and recursion. For most std::string code, std::reverse() is the clearest in-place option, while a second string is appropriate when the original text must be preserved.