翻转句子中单词的顺序

题目:输入一个英文句子,翻转句子中单词的顺序,但单词内字符的顺序不变。句子中单词以空格符隔开。为简单起见,标点符号和普通字母一样处理。

例如输入“I am a student.”,则输出“student. a am I”。

 
 
#include<stdlib.h>
#include<iostream>
#include<stdio.h>
#include<vector>
#include<time.h>
#include<set>
#include<map>
#include<assert.h>
#include<string.h>
#include<stack>
#include<set>
using namespace std;
 
int main()
{
     
    char str[50005];
    while(gets(str))
    {
         
        int len=strlen(str);
        int s;
        bool sf=true;
        for(int i=len-1;i>=0;i--)
        {
            if(str[i]==' ')
            {
                if(sf==true)
                  printf(" ");
                else
                {
                 //
                    for(int j=i+1;j<=s;j++)
                        printf("%c",str[j]);
                    sf=true;
                    printf(" ");
                }
            }
            else
            {
                if(sf==true)
                {
                    sf=false;
                    s=i;
                }
                if(i==0)
                {
                  for(int j=i;j<=s;j++)
                        printf("%c",str[j]);
                }
            }
        }
        printf("\n");
    }
}
/**************************************************************
    Problem: 1361
    User: billforum
    Language: C++
    Result: Accepted
    Time:300 ms
    Memory:1512 kb
****************************************************************/
posted @ 2012-08-27 11:03  wuzhibin  阅读(146)  评论(0编辑  收藏  举报