有一个字符数组的内容为:”student a am i”,请你将数组的内容改为”i am a student”.
要求:
不能使用库函数。只能开辟有限个空间(空间个数和字符串的长度无关)。
#include <stdio.h>
#include <assert.h>
#include <windows.h>
voidstr_switch(char *start, char *end)
{
assert(start);
assert(end);
while (start < end){
*start ^= *end;
*end ^= *start;
*start ^= *end;
start++, end--;
}
}
int main()
{
chararr[] = "student a am i";
char *p = arr;
char *q = p;
while (*p != '\0'){
if (isspace(*p)){
str_switch(q, p-1);
q = p + 1;
}
p++;
}
str_switch(q, p-1);
str_switch(arr, arr+strlen(arr)-1);
printf("res : %s\n", arr);
system("pause");
return 0;
}