How do you reverse a string in C or C++ without requiring a separate buffer to hold the reversed string?
Another C++ way (though I would probably use std::reverse() myself :) as being more expressive and faster)
str = std::string(str.rbegin(), str.rend());
The C way (more or less :) ) and please, be careful about XOR trick for swapping, compilers sometimes cannot optimize that.
In such case it is usually much slower.
char* reverse(char* s)
{
char* beg = s, *end = s, tmp;
while (*end) end++;
while (end-- > beg)
{
tmp = *beg;
*beg++ = *end;
*end = tmp;
}
return s;
} // fixed: check history for details, as those are interesting ones