Write code to reverse a C-Style String. (C-String means that “abcd” is represented as five characters, including the null character.)
两端逼近,同时交换。
char* reverse(char* s)
{
char* start = s;
char* end = s ;
while(*end)
end++;
end -- ;
while(start < end)
{
char t = *start;
*start++ = *end;
*end-- = t;
}
return s ;
}
int main()
{
char s[]="hello";
cout << reverse(s) << endl;
return 0;
}