I am currently studying different algorithms to practice my coding strengths and came across an alogrithm to reverse a string. It uses pointers and I am alittle confused on what is going on. The code is below:
void reverse(char *str) {
char * end = str;
char tmp;
if (str) {
while (*end) {
++end;
}
--end;
while (str < end) {
tmp = *str;
*str++ = *end;
*end-- = tmp;
}
}
}
So in this code, the end is put to the end of the strings memory location in the first while loop. Then the second while is where I am getting confused. I do not understand what is going with the *str++ = *end and *end-- = *temp.
Why is this happening like this? When the line tmp = *str occurs, does the first letter of the string go into temp or am I thinking of this incorrectly?