Word Reversal

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

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

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>
#include <algorithm>
#include<cstdio>
using namespace std;

int main()
{
    int cas;
    cin >> cas;
    getchar();//此处处理回车字符  记住  如果不处理 会出现问题的  
    while (cas--)
   {
    string s, t;
    getline(cin,s);
    t.clear();//每个测试案例之前都要清理掉t 它就是一个类似字符串(就是一个单词)的操作空间 
    int flag=0;
    //char str= ' ';  一个空字符不能用字符数组来表示    
    for ( int i = 0; i < s.length() ; i++)
    {
        //看不太懂你核心代码的意思 有时间可以探讨一下 
    /*    int pos = s.find(str,i);//这儿实现比较复杂  
        cout<<pos;
        int j = s.length() - pos;
        int a;
        for(; a <= pos && j <= s.length(); a++ , j++)
        {
            s[a] = t[j];
        }
        a += pos + 1;*/
        //代码思想是:1、不是空格且没到字符串的长度 就把s中的一个一个字母放到t中 
         //           2、遇到空格  就把它翻转 reverse函数 ;空格 flag来设置, 
        if(s[i]!=' '&&i<=s.length()-1)
              t+=s[i];
        else
        {
            reverse(t.begin(),t.end());            
            if(flag) cout<<" ";
            cout<<t;
            flag=1;//遇到空格 就把flag设置为1 ,等下一个单词t 输出之前 把空格输出来 
            t.clear();//t   也就是一个单词  故处理下一个单词时  把他清理掉 
            
        }
        
    }
    //这部分你的想法是对的     
    reverse(t.begin(),t.end());//最后一个单词  它不会出现空格  故放在for循环外面处理 
    cout<<" "<< t << endl; //输出之前要加空格 
 }

return 0;
} 

 

posted @ 2016-11-12 20:06  泡面小王子  阅读(240)  评论(0编辑  收藏  举报