how can i reverse a string???

is there any function to reverse my string???

Another option:
1
2
3
4
std::string ReverseStr(const std::string& s)
{
  return std::string(s.rbegin(), s.rend());
}
OP: bear in mind that std::reverse() works in place, so the original string will be lost unless you make a copy of it first (or reverse() the reversed string!)
Last edited on
if you want to understand HOW to do it, rather than use the built in..
you can do it a number of ways.

one way is to go to the end of the string and iterate over it backwards, copying into a new string.

you can also grab the first and last character, swap, second and next to last, swap, ... in a loop until your pointers cross or equal each other.

And if you want to do it fast, you can use the cpu provided byte swapper (assembly) designed for the endian integer problem :)


Last edited on
if you want to understand HOW to do it

the cplusplus link also carry the implementation
Topic archived. No new replies allowed.