Reverse a string in C++
This post provides an overview of some of the available alternatives to reverse a string in C++ without modifying the original string.
1. Using the std::reverse algorithm
The standard way to reverse a string in C++ is to use the std::reverse algorithm from the <algorithm> header. This algorithm reverses the order of the elements in a range, such as a string or an array. The reverse algorithm is very powerful and flexible, as it can work with different types of containers and iterators. To use the reverse algorithm to reverse a string in C++, we need to provide two arguments: the beginning and the end of the range. For example:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
#include <iostream> #include <string> #include <algorithm> using namespace std; int main() { // Create a string string s("Reverse string"); // Reverse the string using the reverse algorithm reverse(s.begin(), s.end()); // Print the reversed string cout << s << endl; return 0; } |
Note that the reverse algorithm is simple to use, but modifies the original string in-place, which means that it changes the order of the characters in the original string.
2. Using std::string::rbegin and std::string::rend functions
Another way to reverse a string in C++ is to use the rbegin and rend functions from the std::string class. These functions return reverse iterators that point to the last and the first element of the string, respectively. Reverse iterators are special types of iterators that move backwards instead of forwards when incremented or decremented. To use the rbegin and rend functions to reverse a string in C++, we need to create a new string object and initialize it with the reverse iterators of the original string. For example:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
#include <iostream> #include <string> #include <algorithm> using namespace std; string reverse(string const &s) { string rev(s.rbegin(), s.rend()); return rev; } int main() { string s("Reverse string"); s = reverse(s); cout << s << endl; return 0; } |
3. Using std::string::crbegin and std::string::crend functions
Another option is to loop through the characters of a string backward is by using reverse iterators. Since iteration is read-only, it is recommended to use const reverse iterators returned by crbegin and crend. The crbegin points to the last character of the string, and crend points to the first character of the string. Note that in a reverse iterator, the operator++ moves the iterator by one position to the previous character.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
#include <iostream> #include <string> using namespace std; string reverse(string const &s) { string rev; string::const_reverse_iterator it = s.crbegin(); while (it != s.crend()) { rev = rev.append(1, *(it++)); }; return rev; } int main() { string s("Reverse string"); s = reverse(s); cout << s << endl; return 0; } |
4. Using std::for_each function
We can remove the complexity of iterators with the standard STL algorithm std::for_each, which applies a specified condition to every element in the range defined by the input iterators. Since we’re iterating backward and the iteration is read-only, it is recommended to use const reverse iterators returned by std::string::crbegin and std::string::crend.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
#include <iostream> #include <string> #include <algorithm> using namespace std; string reverse(string const &s) { string rev; for_each(s.crbegin(), s.crend(), [&rev] (char const &c) { rev = rev.append(1, c); }); return rev; } int main() { string s("Reverse string"); s = reverse(s); cout << s << endl; return 0; } |
5. Using simple for-loop
Finally, we can loop through the characters of the given string backward using a simple for-loop and append the corresponding character at each index to a new string using the string::append function. For example:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
#include <iostream> #include <string> using namespace std; string reverse(string const &s) { string rev; for (int i = s.size() - 1; i >= 0; i--) { rev = rev.append(1, s[i]); }; return rev; } int main() { string s("Reverse string"); s = reverse(s); cout << s << endl; return 0; } |
That’s all about reversing a string in C++.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)