【问题描述】
给出一个字符串,请将其每个单词反转后输出。
【输入形式】
输入第一行为一个正整数N,表示测试用例数,接下来的N行,每行一个字符串。
【输出形式】
输出N行,每行对应一个反转后的字符串。
【样例输入】
3 olleh !dlrow m'I morf .unh I ekil .tae
【样例输出】
hello world! I'm from hnu. I like eat.
字符串反转函数reverse
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
int main()
{
int N = 0;
cin >> N;
getchar();
while (N--)
{
string str;
getline(cin, str);
int curr = -1;
while (1)
{
int temp = str.find(' ', curr + 1);
if (temp == -1)
{
string s(str, curr);
reverse(s.begin(), s.end());
cout << s << ' ';
break;
}
else
{
string s(str, curr+1, temp-curr-1);
reverse(s.begin(), s.end());
curr = temp;
cout << s << ' ';
}
}
cout << endl;
}
}