题目传送门 http://acm.hdu.edu.cn/game/entry/problem/list.php?chapterid=1§ionid=2
#include <iostream>
#include <stdio.h>
using namespace std;
int main(){
int t, i, j;
char tmp; //用于交换字符串
cin >> t;
char s1[1001];
getchar(); //吸收回车
while(t--){
gets(s1); //读取一行
//用h找空格
for(i=0, j=0; s1[i] != '\0'; i++){
if(s1[i] == 32){
int h = i-1;
int k = j;
while(k<h){ //对单词进行逆序
tmp = s1[h];
s1[h] = s1[k];
s1[k] = tmp;
k++;
h--;
}
//更新j的值,指向下一个单词的第一个字符
j = i+1;
while(s1[j] == 32){
j++;
if(s1[j] == '\0') break;//结束了就退出
}
}
}
//进行最后一个单词的逆序
i--;
while(j<i){
tmp = s1[i];
s1[i] = s1[j];
s1[j] = tmp;
i--;
j++;
}
cout<<s1<<endl;
}
return 0;
}