#include <iostream>
#include <string>
#include <stdio.h>
using namespace std;
/*
输入一句话,然后把这个字符串以单词为单位,逆转输出。
比如将“Alice call Jack”转换为“Jack call Alice”
123 456 789
789 456 123
第一步:现将单词挨个取反 321 654 987
第二步:整体再次取反 789 456 123
*/
int main(void){
char str[256];
cout << "请输入一句话(英文) " << endl;
gets_s(str,sizeof(str));
int i = 0;
while(str[i]){
//跳过空格
while(str[i] == ' ') i++;
//找到单词的开头
int j = i;
while(str[j] !=' ' && str[j]) j++;
//逆转单词5
for(int k1=i,k2=j-1; k1<k2; k1++,k2--){
char tmp = str[k1];
str[k1] = str[k2];
str[k2] = tmp;
}
i = j;
}
//整体逆转
for(int k1=0,k2=i-1; k1<k2; k1++,k2--){
char tmp = str[k1];
str[k1] = str[k2];
str[k2] = tmp;
}
cout << str ;
system ("pause");
return 0;
}