Example 1:
Input: "hello"
Output: "olleh"
Example 2:
Input: “ProDeveloperTutorial is a good website”
Output: etisbew doog a si lairotuTrepoleveDorP
Method 1: Using STL function.
Method 2: By using swap function.
Solution in C++
#include<iostream>
#include<string>
#include<set>
#include<vector>
using namespace std;
string reverse_string_method_1(string str)
{
return string(str.rbegin(), str.rend());
}
string reverse_string_method_2(string str)
{
int i = 0;
int j = str.size() - 1;
while(i < j)
{
swap(str[i++], str[j--]);
}
return str;
}
int main()
{
string str = "hello";
cout<<"The string is = "<<str<<endl;
cout<<"The reverse by using method 1 is = "<<reverse_string_method_1(str)<<endl;
string str_1 = "ProDeveloperTutorial is a good website";
cout<<"\nThe string is = "<<str_1<<endl;
cout<<"The reverse by using method 1 is = "<<reverse_string_method_2(str_1)<<endl;
}
Output:
The string is = hello
The reverse by using method 1 is = olleh
The string is = ProDeveloperTutorial is a good website
The reverse by using method 1 is = etisbew doog a si lairotuTrepoleveDorP