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.
As an answer:
i
and j
run against each other (from the beginning or end, respectively).
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.