xxxxxxxxxx
string name1 = "Sam" ;
string name2 = name1 ; /// Have to store the ORIGINAL in ANOTHER variable ...then Reverse that
reverse(name2.begin(),name2.end()) ;
cout << name2 ; // maS
xxxxxxxxxx
#include <algorithm>
#include <iostream>
#include <string>
int main()
{
std::string foo("foo");
std::string copy(foo);
std::cout << foo << '\n' << copy << '\n';
std::reverse(copy.begin(), copy.end());
std::cout << foo << '\n' << copy << '\n';
}
xxxxxxxxxx
// C++ function to reverse string str:
string rev = string(str.rbegin(), str.rend());
xxxxxxxxxx
string reverse(string str)
{
string output;
for(int i=str.length()-1; i<str.length(); i--)
{
output += str[i];
}
return output;
}