zoukankan      html  css  js  c++  java
  • 单词倒排

    题目描述

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

    说明:

    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]='';
    
            cout<< op<<endl;
    
            stack<string>().swap(st);
        }
        system("pause");
    }
  • 相关阅读:
    C/C++利用gsoap库调用WebService
    常用的一些SQL语句
    C# 异步编程
    jQuery学习基础总结
    LINQ↔Lambda↔SQL 互相“翻译”
    NPOI 利用DataTable导出Excel 2003和2007
    图像二值化处理
    spring整合JMS
    RestTemplate使用
    Spring数据源配置
  • 原文地址:https://www.cnblogs.com/achao123456/p/7449997.html
Copyright © 2011-2022 走看看