本题比较基础,首先读入一行字符串,然后从前到后读取各个字符,如果不是空格,则记录从前一个空格到现在位置的字符数;如果是空格,则倒序输出现在位置到前一个空格之间的字符。
注意每一行结尾都要输出换行符,包括最后一行。
#include <iostream>
#include <string>
#include <cstring>
#include <cctype>
using namespace std;
int main()
{
int n;
cin >> n;
cin.get(); //消除数字之后输入的换行符
while (n--)
{
string str;
getline(cin, str); //读入一行字符串
int len = str.length();
int temp = 0;
for (int i = 0; i <= len; i++)
{
if (i == len) //到达字符串结尾
{
for (int j = i - 1; j >= i - temp; j--)
cout << str[j];
cout << endl;
break;
}
if (!isspace(str[i])) //不是空格
{
++temp;
}
else //是空格
{
for (int j = i - 1; j >= i - temp; j--)
cout << str[j];
cout << " ";
temp = 0;
}
}
}
return 0;
}
继续加油。