This article will explain 3 different programs in C++ to reverse a string with output.

Reversing a string means placing the last character at first position, second last at second position and so on.
Example of reverse string is overloading and gnidaolrevo.

Method 1: Using reverse() function
C++ String class has an inbuilt function reverse().
This function accepts two iterators as arguments.
First argument is an iterator that points to the start of the string, and
Second argument iterator that points to the end of the string.
Iterators pointing to the start and end of a string can be retrieved using its begin() and end() functions.
Example program is given below.

#include <iostream>
#include <string>

using namespace std;

int main()
{
    string str = "Reversing a string";
    cout << "Original String: " << cap << endl;
    reverse(str.begin(), str.end());
    cout << "Reversed String: " << rev << endl;
}

Output of this program is

Original String: Reversing a string
Reversed String: gnirts a gnisreveR

Method 2: Using for loop
This method is based on following steps:

1. Initialize an empty string to hold the reversed string.
2. Initialize an integer variable to 0 which represents the index of characters in the string in Step 1.
2. Iterate over the original string in reverse direction using a for loop.
3. In each iteration, assign the current character to the string initialized in Step 1 and increment the variable defined in Step 2.
Thus, we are assigning a character from the end of the original string to the start of a new string.
After the loop completes, the string defined in Step 1 will have reversed contents.

Example program based on this algorithm is given below.

#include <iostream>
#include <string>

using namespace std;

int main()
{
    string str = "Reversing a string";
    // string to hold reverse
    string rev = "";
    // get the length of original string
    int length = str.length();
    // make the empty string of same size
    rev.resize(length);
    // integer for reverse string
    int counter = 0;
    // loop over string in reverse
    for (int i = str.length() - 1; i >= 0; i--)
    {
        // assign character to new string
        rev[counter] = str[i];
        // increase the character index
        counter++;
    }
    cout << "Original String: " << str << endl;
    cout << "Reversed String: " << rev << endl;
}

This prints

Original String: Reversing a string
Reversed String: gnirts a gnisreveR

Notice that for loop will iterate from the length of the string till 0, since it needs to be in reverse direction.

Method 3: Using string constructor
A constructor is used to make an object of a class. Every string in C++ is an object of String class.
String class has a constructor which takes two reverse iterators as arguments. We can get reverse iterators to a string using its rbegin() and rend() functions.

Example program follows.

#include <iostream>
#include <string>

using namespace std;

int main()
{
    string str = "Reversing a string";
    // create a string with its constructor
    string rev = string(str.rbegin(), str.rend());
    cout << "Original String: " << str << endl;
    cout << "Reversed String: " << rev << endl;
}

Output of this program will be

Original String: Reversing a string
Reversed String: gnirts a gnisreveR

Hit the clap if the article helped you out.

Leave a Reply