Write a function that takes a string as input and returns the string reversed.
Example: Given s = "hello", return "olleh".
思路
- 双指针
char* reverseString(char* s) {
char *start = s;
char *end = s + strlen(s) - 1; // 注意这里 -1,比如 s = "abc\0", strlen(s)=3, s+3 ---> '\0';
while(start < end) { // 无需验证 s 是否为 NULL
char tmp = *start;
*start = *end;
*end = tmp;
start++;
end--;
}
return s;
}