The code below returns a reversed string. For example, it take input "codebyte
" and returns "etybedoc
".
string FirstReverse(string str) {
for(int i = 0, j = str.length() - 1; i < str.length() / 2; i++, j--)
{
str[i]^=str[j]^=str[i]^=str[j];
}
return str;
}
I am lost as to how this function works:
- Why is the
^=
-operator being used? It is a bitwise operator but why is it being used here? - Why is
str.length()
divided by 2 in the for loop? - What is with the alteration of
str[i]
andstr[j]
?
I want to work though it with values but I don't know where to begin. The introductory textbook I used did not cover this.