使用reverse函数, 换行符判断使用 str[i] == '\0'。
#include <iostream>
#include <algorithm>
using namespace std;
char str[1010];
int main()
{
int t;
cin >> t;
getchar();
while(t--)
{
int t = 0;
while(gets(str))
{
int n = strlen(str);
int index = 0;
for(int i = 0 ;i <= n; i++)
{
if(str[i] == ' ' || str[i] == '\0')
{
reverse(str + index, str + i);
index = i + 1;
}
}
cout << str << endl ;
}
}
return 0;
}