Here is another variation that reverses the string in place.
Code:
#include <stdio.h>
#include <string.h>
char *mystrrev(char *s)
{
char *start = s, *t = strchr(s, '\0'); /* point to end of string */
/*
* Swap the values at the beginning (pointed to by 's') and the end
* (pointed to by 't'); 's' and 't' meet in the middle.
*/
for ( --t/* skip terminating null character */; s < t; ++s, --t )
{
/* Just your run-of-the-mill swap here. */
char temp = *s;
*s = *t;
*t = temp;
}
return start;
}
int main(void)
{
char text[] = "Hello world";
puts(text);
puts(mystrrev(text));
return 0;
}
/* my output
Hello world
dlrow olleH
*/