Question
Write a function that takes a string as input and returns the string reversed.
Example:
Given s = "hello", return "olleh".
Solution
异或交换首尾
char* reverseString(char* s) {
int i, start = 0, end = strlen(s) - 1;
while(start < end) {
s[start] = s[start]^s[end];
s[end] = s[start]^s[end];
s[start] = s[start]^s[end];
start++;
end--;
}
return s;
}
char* reverseString(char* s) {
int i, len = strlen(s);
char *ss = (char*)calloc((len+1),sizeof(char));
for (i = 0; i < len; i++) {
ss[i] = s[len-i-1];
}
return ss;
}