Reputation: 909
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:
^=
-operator being used? It is a bitwise operator but why is it being used here?str.length()
divided by 2 in the for loop?str[i]
and str[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.
Upvotes: 2
Views: 218
Reputation: 780
As an answer:
i
and j
run against each other (from the beginning or end, respectively).Upvotes: 3