1151_Word Reversal

来源:互联网 发布:复制手机联系人软件 编辑:程序博客网 时间:2024/06/05 11:11

For each list of words, output a line with each word reversed without changing the order of the words.


This problem contains multiple test cases!

The first line of a multiple input is an integer N, then a blank line followed by N input blocks. Each input block is in the format indicated in the problem description. There is a blank line between input blocks.

The output format consists of N output blocks. There is a blank line between output blocks.


Input

You will be given a number of test cases. The first line contains a positive integer indicating the number of cases to follow. Each case is given on a line containing a list of words separated by one space, and each word contains only uppercase and lowercase letters.


Output

For each test case, print the output on one line.


Sample Input

1

3
I am happy today
To be or not to be
I want to win the practice contest


Sample Output

I ma yppah yadot
oT eb ro ton ot eb
I tnaw ot niw eht ecitcarp tsetnoc


××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××


#include<iostream>

#include<string>
using namespace std;
int main()
{
string line;
int count=0;
int num;
int num_1;
cin>>num;
for(int m=0;m!=num;++m)
{   
cin>>num_1;
for(int n=0;n!=num_1+1;++n)//循环次数少一次,怎么回事?
{
getline(cin,line);
if(n==0)//输入num_1后,然后又一个换行符,将此行掠过,浪费一个getline();所以n!=num_1+1;比正常大一。
  continue;
for(int i=0;i!=line.size();i++)
{
if(line[i]==' ')
{
for(int j=i-1;j>=count;j--)
cout<<line[j];
cout<<' ';
count=i+1;
}
if(i==line.size()-1)
{
for(int j=i;j>=count;j--)
cout<<line[j];
}
}
cout<<endl;
count=0;
cin.clear();
}
if(m!=num-1)
  cout<<endl;
}




return 0;
}
//c++ string getline()函数
//一个有用的string IO操作:getline。这个函数接受两个参数:一个输入流对象和一个string对象。getline函数从输入流的下一行读取,并保存读取的内容到 string中,但不包括换行符。和输入操作符不一样的是,getline并不忽略行开头的换行符。只要getline遇到换行符,即便它是输入的第一个 字符,getline也将停止读入并返回。如果第一个字符就是换行符,则string参数将被置为空string。
//getline函数将istream参数作为返回值,和输入操作符一样也把它用作判断条件。
//由于line不含换行符,若要逐行输出需要自行添加。照常,用endl来输出一个换行符来刷新输出缓冲区。
//注解:由于getline函数返回时丢弃换行符,换行符将不会存储在string对象中。
//摘自《c++ Primer》