题目
给定一串字符"I love china",实现以单词为单位的逆序,如:"china love i"
#include <stdio.h>
#include <strings.h>
#include <string.h>
int main(int argc, char const *argv[])
{
char s[] = "i love china"; // china love i
// 1.整个字符串倒置
// abcd
char *p1 = s;
char *p2 = &s[strlen(s) - 1];
while (p2 > p1)
{
*p1 ^= *p2; //*p1=*p1^*p2
*p2 ^= *p1;
*p1 ^= *p2;
p1++;
p2--;
}
puts(s);
// 每个单词倒置
p1 = p2 = s; // anihc evol i
while (*p2 != '\0')
{
while (*p2 != 32 && *p2 != '\0')
{
p2++;
}
char *k = p2; // 保存空格
p2--;
while (p2 > p1)
{
*p1 ^= *p2; //*p1=*p1^*p2
*p2 ^= *p1;
*p1 ^= *p2;
p1++;
p2--;
}
while (*k == 32)
{
k++;
}
p1 = p2 = k;
}
puts(s);
return 0;
}