How To Create The Reverse a String C++?
In C++, to reverse a string or perform a complete flip on it involves altering the sequence of characters, resulting in a version where the reading occurs backward.
Different Methods to Reverse a String in C++ are:
- Using built-in reverse function
- Using Constructor
- Using a temp String
- Making custom function
- Using strrev()
1. Using the built-in reverse function
Within C++, there exists a built-in reverse function designed for reversing strings. Invoking this function necessitates providing two inputs:
In C++, to reverse a string or perform a complete flip on it involves altering the sequence of characters, resulting in a version where the reading occurs backward.
The code snippet below shows how:
#include <iostream>
//The library below must be included for the reverse function to work
#include<bits/stdc++.h>
using namespace std;
int main()
{
string greeting = "Hello";
//Note that it takes the iterators to the start and end of the string as arguments
reverse(greeting.begin(),greeting.end());
cout<<greeting<<endl;
}
Output:
olleH
2. Reverse a String Using the Constructor
Using reversed iterators as inputs for the constructor gives us a flipped string.
The code snippet below shows how:
// C++ program to reverse
// string using constructor
#include <bits/stdc++.h>
using namespace std;
int main()
{
string str = "Hello";
// Use of reverse iterators
string rev = string(str.rbegin(), str.rend());
cout << rev << endl;
return 0;
}
Output:
olleH
3. Using a Temporary String
A more indirect way to flip a string is by moving through it in reverse and collecting the elements in a new string that’s the same size.
To add elements to an empty string, we can use the push_back method.
The code snippet below shows how:
#include <iostream>
using namespace std;
int main()
{
string greeting = "Hello";
string new_greeting;
for(int n = greeting.length()-1; n >= 0; n--)
{
new_greeting.push_back(greeting[n]);
}
cout<<"Original string: "<< greeting << endl;
cout<<"New reversed string: "<< new_greeting << endl;
}
Output:
olleH
4. Using a function
A function can also be written to reverse a string using recursion.
The code snippet below shows how:
#include <iostream>
using namespace std;
void reverse_String(string& greet, int n,int i)
{
if(n<=i){return;}
swap(greet[i],greet[n]);
reverse_String(greet,n-1,i+1);
}
int main()
{
string greeting = "Hello";
cout<<"String before reversal: "<<greeting<<endl;
reverse_String(greeting,greeting.length()-1,0);
cout<<"String after reversal: "<<greeting<<endl;
}
Output
String before reversal: Hello
String after reversal: olleH
5. Using strrev()
The `strrev()` function is a built-in feature within C++. It is located in the `cstring.h` header file and reverses a C-string (an array of characters). Importantly, all that is needed as input is the starting memory address of the string. Once provided, the function effectively reverses the string. Let’s explore its usage to understand better the application of the `strrev()` function in C++.
#include<iostream>
#include<cstring>
int main()
{
char str[] ="Hello"; strrev(str); cout<<"n"<<str; return 0;
}
Output:
olleH