码迷,mamicode.com
首页 > 其他好文 > 详细

单词倒排

时间:2017-08-29 20:39:31      阅读:170      评论:0      收藏:0      [点我收藏+]

标签:pop   empty   get   swap   单词   color   pause   esc   cout   

题目描述

对字符串中的所有单词进行倒排。

说明:

1、每个单词是以26个大写或小写英文字母构成;

2、非构成单词的字符均视为单词间隔符;

3、要求倒排后的单词间隔符以一个空格表示;如果原字符串中相邻单词间有多个间隔符时,倒排转换后也只允许出现一个空格间隔符;

4、每个单词最长20个字母;

输入描述:

输入一行以空格来分隔的句子

输出描述:

输出句子的逆序

示例1

输入

I am a student

输出

student a am I

/*
    stl stack study
*/
#include <iostream>
#include <stack>
#include <string>
#include <cctype>  /*isalpha*/

using namespace std;

int main(void)
{
    string inp;
    stack<string>st;
    while(getline(cin,inp))
    {
        bool f=false;
        int i=0;string tmp;
        for (i = 0; i <= inp.length(); i++)
        {
            if (isalpha(inp[i]))
            {
                tmp+=inp[i];
                f=true;
            }
            else
            {
                if(f==true)
                {
                    st.push(tmp);
                    tmp.clear();
                    f=false;
                }
            }
        }

        string op;

        while (!(st.empty()))
        {
            op+=st.top()+" ";
            st.pop();
        }

        op[op.length()-1]=\0;

        cout<< op<<endl;

        stack<string>().swap(st);
    }
    system("pause");
}

 

 

单词倒排

标签:pop   empty   get   swap   单词   color   pause   esc   cout   

原文地址:http://www.cnblogs.com/achao123456/p/7449997.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!