Can you please explain the order of operations for the following function that reverses a string?
//http://www.perlmonks.org/?node_id=589993 source of method
/* reverse a string in place, return str */
static char* reverse(char* str)
{
char* left = str;
char* right = left + strlen(str) - 1;
char tmp;
while (left < right)
{
tmp = *left;
*left++ = *right;//This part is real confusing...Does ++ happen after the assignment?
*right-- = tmp;//This one is just as bad
}
return str;
}
I can follow the first 3 lines in the method pretty easily, but once it hits the while loop I am at a loss for how this works. Specifically the lines marked above. Thank you.
In this statement,
*leftis assigned*right, and then after all of thatleft(not*left) is incremented. The same logic goes for*right--So rewritten, the pseudocode would be this: