原文:
Write code to reverse a C-Style String. (C-String means that “abcd” is represented as five characters, including the null character.)
写代码翻转一个C风格的字符串。(C风格的意思是"abcd"需要用5个字符来表示,包含末尾的 结束字符)
思路:用指针头尾指针交换即可。
void reverse(char *s){
if(!s) return;
char *p = s, *q = s;
while(*q) ++q;
--q;
while(p < q)
swap(*p++, *q--);
}
swap的异或写法
void swap(char &a, char &b){
a = a^b;
b = a^b;
a = a^b;
}
参见:http://hawstein.com/posts/1.2.html