定义一个指针指向字符串开始,把它移到最后,不停交换首尾两个指针指向的字符串就好了,一直到两个指针相遇.
void reverse(char *s)
{
char *end = s;
if(s)
{
while(*end)
end++;
end--;//因为++,end最后会指向'/0',也就是*end的值是0,所以要再--
while(s<end)
{
char tmp = *s;
*s++ = *end;
*end-- = tmp;
}
}
}
int main(void)
{
char s[] = "123456?&@?";
reverse(s);
printf("%s/n", s);
return 0;
}