In this C++ tutorial, you will learn how to reverse a string using reverse() function, or looping statement, with examples.

C++ String Reverse

You can reverse a string in C++ either by using available reverse() builtin function, or you can write your own logic using looping techniques.

1. C++ String Reverse using reverse()

reverse() is a function in algorithm header file used to reverse a sequence in the given range.

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

#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
ADVERTISEMENT

2. Reverse a string by swapping characters

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

#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

3. Reverse a string by copying string from right to left into a new string

To reverse a string by swapping, 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

#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

4. Reverse a string using Recursion

In the following example, we shall write a recursion function that reveres a given string.

C++ Program

#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

Conclusion

In this C++ Tutorial, we learned how to reverse a string using inbuilt functions, and looping techniques.