I dont really understand what the last while loop is doing, can someone explain it?
void reverse(char *str) {
char * end = str;
char tmp;
if (str) {
while (*end) {
++end;
}
--end;
while (str < end) {
tmp = *str;
*str++ = *end;
*end-- = tmp;
}
}
}
Can someone walk me through the example “hello”?
What’s basically happening in that innermost while loop is that in each iteration, the characters pointed to by
strandendget swapped,strgets incremented to point to the next character, andendis decremented to point to the previous one.Using “hello” as an example:
And then the loop ends, as
str=end.