原文:
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个字符来表示,包含末尾的 结束字符)
C style string是指结尾有‘\0’结束符
void reverse(char *str) {
if (NULL == str) exit(-1);
char *end;
char tmp;
while (*end) {
end++;
}
end--;
while (str < end) {
tmp = *end;
*end-- = *str;
*str++ = tmp;
}
}