How to Reverse a String in C++

  1. Use String Constructor to Reverse a String
  2. Use std::reverse() Algorithm to Reverse a String
  3. Use std::copy() Algorithm to Reverse String

This article will explain how to reverse a string in C++.

Use String Constructor to Reverse a String

std::basic_string has the constructor, which can build a string with the contents of the range. We can then declare a new string variable and feed its constructor the reverse iterators of the original string variable - tmp_s. The following example demonstrates this method and outputs both strings for verification.

#include <iostream>
#include <string>
#include <algorithm>

using std::cout; using std::endl;
using std::string; using std::reverse;

int main(){
    string tmp_s = "This string will be reversed";
    cout << tmp_s << endl;

    string tmp_s_reversed (tmp_s.rbegin(), tmp_s.rend());
    cout << tmp_s_reversed << endl;

    return EXIT_SUCCESS;
}

Output:

This string will be reversed
desrever eb lliw gnirts sihT

Use std::reverse() Algorithm to Reverse a String

The std::reverse method is from the <algorithm> STL library, and it reverses the order of the elements in the range. The method operates on objects passed as arguments and does not return a new copy of the data, so we need to declare another variable to preserve the original string.

Note that the reverse function throws std::bad_alloc exception if the algorithm fails to allocate memory.

#include <iostream>
#include <string>
#include <algorithm>

using std::cout; using std::cin; using std::endl;
using std::string; using std::reverse;

int main(){
    string tmp_s = "This string will be reversed";
    cout << tmp_s << endl;

    string tmp_s_reversed(tmp_s);
    reverse(tmp_s_reversed.begin(), tmp_s_reversed.end());
    cout << tmp_s_reversed << endl;

    return EXIT_SUCCESS;
}

Use std::copy() Algorithm to Reverse String

std::copy is another powerful algorithm, which can be utilized for multiple scenarios. This solution initializes a new string variable and modifies its size using the builtin resize method. Next, we call the copy method to fill the declared string with the original string’s data. Note, though, the first two parameters should be reverse iterators of the source range.

#include <iostream>
#include <string>
#include <algorithm>

using std::cout; using std::cin; using std::endl;
using std::string; using std::reverse;

int main(){
    string tmp_s = "This string will be reversed";
    cout << tmp_s << endl;

    string tmp_s_reversed;
    tmp_s_reversed.resize(tmp_s.size());
    copy(tmp_s.rbegin(), tmp_s.rend(), tmp_s_reversed.begin());
    cout << tmp_s_reversed << endl;

    return EXIT_SUCCESS;
}

In case the reversed string data need not be stored, we can use the copy() algorithm to directly output the string data in reverse order to the console as shown in the following code sample:

#include <iostream>
#include <string>
#include <algorithm>

using std::cout; using std::cin; using std::endl;
using std::string; using std::reverse;

int main(){
    string tmp_s = "This string will be reversed";
    cout << tmp_s << endl;

    copy(tmp_s.rbegin(), tmp_s.rend(),
         std::ostream_iterator<char>(cout,""));

    return EXIT_SUCCESS;
}

Related Article - C++ String

  • How to Find Substring in String in C++
  • How to Convert Char Array to String in C++
  • comments powered by Disqus