3 Ways of reverse a string in C++

Featured image for reversing a string in C++ Tutorial

Reverse a string in C++

You may use different ways for reversing a string in C++. This includes built-in functions that you may use quite easily and you may write your function as well.

First way – Using reverse function example

  • There is a built-in function reverse() that you may use to reverse a string in C++.
  • This is defined in the algorithm header file that you have to include in the C++ file.
  • The reverse function takes two parameters as shown in the example below
#include <algorithm>

#include<iostream>

#include<string>

using namespace std;

int main()

{

    //Creating a string to reverse

    string str_rev = "ABC DEF GHI JKL MNO PQR STU VWX YZ";


    cout <<"The original string: " <<str_rev <<"\n\n";

    //Using reverse function
    reverse(str_rev.begin(), str_rev.end());

    //Display result
    cout <<"The string after reverse function: " <<str_rev <<"\n\n";

    return 0;

}

Output:

CPP-reverse-string

In the program,

  • A string variable is declared and assigned a string containing A-Z.
  • We used the reverse() function with begin and end parameters
  • The string is displayed before and after using the reverse() function.

Using for loop with swap() function

In this example, the swap() function is used to reverse a string where a for loop is used. See the code and output below:

#include <bits/stdc++.h>
using namespace std;

//User defined function that takes one parameter i.e. string to reverse

void rev_function(string& s){

   int str_len = s.length();

   //For loop that uses swap function

   for (int x = 0; x < str_len / 2; x++)

      swap(s[x], s[str_len - x - 1]);

}

int main(){

   string str_rev = "Hello World! C++";


   cout <<"The original string: " <<str_rev <<"\n\n";

   rev_function(str_rev);

   cout <<"The original string: " <<str_rev <<"\n\n";

   cout << str_rev;

   return 0;

}

Output:

CPP-reverse-for

You see, we sent “Hello World! C++” to the function and it reversed the string.

The swap() function is used to swap the characters.

Learn more about the swap function here.

Reading string last to first – for loop

In this approach, a for loop is used and the string is read from last to first to get the reversed string.

Have a look at the code and output:

#include<iostream>

#include<string>

using namespace std;

int main()

{

    string str = "String Reverse Tutorial";

    int i = str.length();

    cout <<"Reversed String: ";

    for(i = i - 1; i >= 0; i--)

    {

                cout<<str[i];

    }


    cout <<"\n\nOriginal String: " <<str <<"\n\n";

    return 0;

}

Output:

CPP-reverse-first-last

The logic:

  • First, you get the length of the string and assign it to an int-type variable in the for loop.
  • The variable i value is decremented in each iteration
  • The output shows a reverse string, as well as original string, remains the same

The example of using a while loop for reversing the string

If you are a fan of the while loop, the same thing can be achieved as in the above example by using while loop.

See the program below where the user is asked to enter a string to be reversed.

The while loop is used to reverse it from last to first and we used getline() function with cin.

#include<iostream>

#include<string>

using namespace std;

int main()

{

    string str;

    //Taking user input for the string to be reversed

    cout<< "Please enter a string to reverse? ";


    //Using getline as simple cin will cut from space

    getline(cin, str);


    int i = str.length() - 1;

    cout<<"Your entered string: " <<str <<"\n\n";

    cout <<"Reversed String: ";


    //while loop to reverse the string

    while (i >= 0) {

        cout<<str[i];

        i--;

    }

    cout <<"\n\nOriginal String: " <<str <<"\n\n";

    return 0;

}

Sample output:

CPP-reverse-while-loop

Reverse string by using do-while loop

And finally, the same can be done by using a do-while loop as follows:

#include<iostream>
#include<string>

using namespace std;

int main()
{
    string str;
    //Taking user input for the string to be reversed
    cout<< "Please enter a string to reverse? ";
    //Using getline as simple cin will cut from space
    getline(cin, str);

    int i = str.length() - 1;

    cout<<"Your entered string: " <<str <<"\n\n";
    cout <<"Reversed String: ";

//Using do-while
    do{
      cout<<str[i];
      i--;
    }
     while (i >= 0) ;
    cout <<"\n\nOriginal String: " <<str <<"\n\n";
    return 0;
}

 

Sample output:

CPP-reverse-do-while

 

Author - Atiq Zia

Atiq is the writer at jquery-az.com, an online tutorial website started in 2014. With a passion for coding and solutions, I navigate through various languages and frameworks. Follow along as we solve the mysteries of coding together!