2012-11-18, 10:18 PM
There is a faster way to do this.
But if you want to do it manually..
Code:
string reverse(string s)
{
return string(s.rend(), s.rbegin());
}But if you want to do it manually..
Code:
string reverse(string s)
{
char temp;
int length = s.length() - 1;
int numSwaps = s.length() >> 1;
for(int i = 0; i < numSwaps; i++)
{
temp = s[i];
s[i] = s[length-i];
s[length-i] = temp;
}
return s;
}