C++

String Reverse in C++

In many C++ programming scenarios, it may be necessary to reverse a string. Reversing a string involves changing the order of the characters in the string and setting it in the reverse order or it could just be printing a string backward. All that is required to reverse a string is to move its final element to the string’s initial place and so on. In some situations, it may be required to reverse the string at its address permanently. C++ provides various methods for reversing the string.

Techniques for Reversing a String in C++ Programming

Depending on the code specifications that we are developing, several techniques exist to implement the string reversal in C++ programming. These techniques are:

  • Utilizing the “reverse()” method
  • Utilizing the “strrev()” method
  • Utilizing the “for” loop
  • Utilizing the “while” loop
  • Utilizing the constructor

Example 1:

First, we import three distinct header files which are “algorithm”, “iostream”, and “string”. We must include these header files so we utilize the functions that are defined in them. The “algorithm” header file includes numerous built-in functions for finding, sorting, counting, modifying, etc. that are available to us.

Then, the “iostream” provides functions for inputting or outputting the data, and the “string” header file is included as it provides the functions needed while working with the string data. Underneath these header files, we add the “std” namespace. Here, we invoke the “main()” function.

Then, we place the “string” data type and initialize the “originalStr” here with some string data. Then, we print that string. After this, we utilize the “reverse()” method that aids in reversing the string. In this “reverse()” method, we add the “begin()” and “end()” along with the “originalStr” variable. Now, we also print this reversed string that we get after applying the “reverse()” method.

Code 1:

#include <algorithm>
#include<iostream>
#include<string>
using namespace std;
int main()
{
    string originalStr = "String Reverse In C++ Programming";
    cout << "The original string!" << endl;
     cout << originalStr << endl << endl;
     reverse(originalStr.begin(), originalStr.end());
     cout<< "The reversed string!" << endl;
    cout<< originalStr;
    return 0;
}

Output:
The reversed string that we get after applying the “reverse()” method in our code is now rendered along with the original string.

Example 2:

We include the header files first and then place the “namespace std”. Then, in the “main()” method, we initialize the character array by placing the “char” data type with the “Org_str” variable name and type the string here that we want to reverse. Then, we render this “Org_str” with the help of “cout”.

Underneath this, we utilize the “strrev()” method to reverse the string and pass “Org_str” as the parameter in this function. Now, the string is reversed here. After this, we also render the reversed string with the aid of “cout”.

Code 2:

#include<iostream>
#include<cstring>
using namespace std;
int main()
{
    char Org_str[] ="String Reverse Program";
    cout << Org_str << endl << endl;
    strrev(Org_str);
    cout << "Reversed String" << endl;
    cout<<Org_str;
    return 0;
}

Output:
Here, the original and reversed strings that we obtained from using the “strrev()” method in our code are rendered.

Example 3:

In this case, the header files are included before the “namespace std” is added. Then, the “main()” is invoked and the string variable is added with the name “myOrgStr” and initialize with the string data.

After this, we also declare the “int” variable “a” and render the “myOrgStr” string using “cout”. Below this, we utilize the “for” loop where we initialize the “a” variable with “myOrgStr.length() – 1” and then place a condition which is “a >= 0” and decrement it in the value of “a”. This reverses our string and stores it in “myOrgStr[a]” and also display it as we place “myOrgStr[a]” inside the “cout”.

Code 3:

#include<iostream>
#include<string>
using namespace std;
int main()
{
    string myOrgStr="Hello World!";
    int a;
    cout << myOrgStr << endl << endl ;
    cout<<"Reversed String" <= 0; a--)
    {
        cout<<myOrgStr[a];
    }
    return 0;
}

Output:
This renders both the original and reversed strings that we got via our code using the “for” loop method.

Example 4:

Here, we import the “bits/stdc++.h” header file so we don’t need to import the other header files since this header file contains all the function’s definitions. Then, we type the “namespace std”. Here, we create a function with the name “Rev()” in which we pass the “string& myString” as the argument of this function. We create this function here to reverse the string.

In this function, we place the “S_len” variable of the “int” data type and initialize it with the “length()” function by placing “myString” with this. Then, we have another variable which is “no” of “int” data type and initialize it with “S_len-1”.

Underneath this, one more variable is initialized which is named “a” of the “int” data type. Here, we utilize the “while()” loop and add “a <= no” as the condition. Then, we utilize the “swap()” method. This “swap()” method aids in swapping the string data and then initializes “no” with the “no -1”. We also initialize “a” with “a+1”.

We then invoke the “main()” here in which we initialize the “myString” variable with the string data and print that string. After this, we call the “Rev()” function that we created in this code and put “myString” as the parameter of this function which reverses the string and then display the reversed string.

Code 4:

#include <bits/stdc++.h>
using namespace std;
void Rev(string& myString)
{
    int S_len = myString.length();
    int no = S_len-1;
    int a = 0;
    while(a <= no){
        swap(myString[a],myString[no]);
        no = no-1;
        a = a+1;
}

}
int main()
{
    string myString = "I love Programming";
    cout << myString << endl;
    cout << "\nReversed String" << endl;
    Rev(myString);
    cout << myString;
    return 0;
}

Output:
Here, we present both the original string and the reversed string that we got by utilizing the “while()” loop and the “swap()” method inside the function that we created in our code.

Example 5:

We import the “bits/stdc++.h” header file with all the function definitions. Therefore, we don’t need to import any other header files. Next, we enter the “namespace std” and call “main()” here. Then, we have a variable “data” of the “string” data type and initialize it with the string that we want to reverse.

We render this original string before reversing it by placing the “data” variable in the “cout”. Underneath this, we initialize another variable which is “revStr” of the same “string” data type. Then, we utilize the “rbegin()” and “rend()” which are the reverse iterators that we add for reversing the string here. The reversed string is now saved in the “revStr” variable that is placed in the “cout” to print the reversed string here.

Code 5:

#include
using namespace std;
int main()
{
    string data = "C++ is the best programming language";
    cout << data << endl << endl;
    string revStr = string(data.rbegin(), data.rend());

    cout << revStr << endl;
    return 0;
}

Output:
The string that we added to the code is first rendered here. Then, the reversed string that we obtained by utilizing the reverse iterators is displayed in the following:

Conclusion

The “string reverse” concept in the C++ programming is discussed in this article where we explored multiple techniques for reversing the string. We explored all methods that help reverse the string in C++ in detail and reversed the string in our C++ codes. In this article, we showed the original and reversed strings in our codes.

About the author

Omar Farooq

Hello Readers, I am Omar and I have been writing technical articles from last decade. You can check out my writing pieces.