方法一:使用变量;
char *change( char *string)
{
assert(string);
//0123456789
char* left = string;
char* right = string + strlen(string)-1;
while (left < right){
char tmp = *left;
*left = *right;
*right = tmp;
left++;
right--;
}
return string;
}
int main()
{
char str[] = { "0123456789" };
printf("before: %s\n", str);
printf("after: %s\n", change(str));
system("pause");
return 0;
}
方法二:不使用变量
char *change( char *string)
{
assert(string);
char *str = string;
int len = strlen(str)-1;
int i = 0;
for (i = 0; i < len - i; i++)
{
str[i] = str[i] ^ str[len - i];
str[len - i] = str[i] ^ str[len - i];
str[i] = str[i] ^ str[len - i];
}
str[len + 1] = '\0';
return str;
}
int main()
{
char str[] = { "0123456789" };
printf("before: %s\n", str);
printf("after: %s\n", change(str));
system("pause");
return 0;
}