Table of Contents
Reverse the given string.
Example
Method 1
Algorithm
Use inbuilt STL functions. (Standard library functions)
Reverse(position 1, position 2) → Reverses from position 1 to position 2
Let string be S
Call function like this: reverse(S.begin(), S.end())
Print S, it will be reversed.
C++ Program
#include <bits/stdc++.h> using namespace std; int main() { string s; //two string cin>>s; reverse(s.begin(),s.end()); //use stl reverse function cout<<s; return 0; }
Method 2
Algorithm
Till the mid element, swap the first and last, second and second last and so on.
Algorithm working
C++ Program
#include <bits/stdc++.h> using namespace std; int main() { string s; //two string cin>>s; for(int i=0; i < s.size()/2; i++) //loop till half and swap the first and last element, second and second last element and so on swap(s[i], s[s.size() - i -1]); cout<<s; return 0; }