Simple: your reverse method doesn't return a value:
char *reverse(char *s)
{
int l, i;
char *begin_ptr, *end_ptr, ch;
l = strlen(s);
begin_ptr = s;
end_ptr = s;
for (i = 0; i < l - 1; i++)
end_ptr++;
for (i = 0; i < l / 2; i++) {
ch = *end_ptr;
*end_ptr = *begin_ptr;
*begin_ptr = ch;
begin_ptr++;
end_ptr--;
}
}
You are actually reversing the string inplace - so the output your see: "dlrow olleh" is coming from your first
printf
, not your second.
A much, much better idea would be to do the reverse into a new buffer and to return that (using
malloc
as in your other function), but you can just add
return s;
to the end of the function.