/*
* Reverse a string
*/
void revstr(char *s) {
int i;
char *temp, *r, *t;
if (strlen(s) == 1)
return;
r = s;
t = s + strlen(s) -1;
while (t > r) {
*temp = *r;
*r = *t;
*t = *temp;
t--;
r++;
}
}
Anonym
12. Okt. 2010
local variable i is not used at all
strlen called twice
pointers can be incremented while accessing
char * revstr (char * string )
{
if ( NULL == string ) return NULL;
char *start = string; // Save point to beginning
char *left = string;
char ch;
while (*string++) // Find end of string
;
string -= 2; // Hop back over zero terminator
while (left < string)
{
ch = *left;
*left++ = *string;
*string-- = ch;
}
return(start);
}