题解 | #单词倒排#

单词倒排

http://www.nowcoder.com/practice/81544a4989df4109b33c2d65037c5836

越来越觉得自己不适合做程序员

#include <iostream>
using namespace std;
int main()
{
    string str, str1;
    bool space = true;
    int i;
    getline(cin, str);
    str1 =  "";
    for (i = str.length() - 1; i >= 0; i--) {
        if ((str[i] >= 'a' && str[i] <= 'z') ||
            (str[i] >= 'A' && str[i] <= 'Z')) {
            str1 = str[i] + str1;
            space = true; //最后一个是字母,遇到非字母或空格可以输出为空格
        } else {
            if (space) {
                cout << str1 << " ";
                str1 = "";
                space = false; //输出个空格之后下个空格或非字母不输出空格
            }
        }
    }
    //最后一个字母需要输出
    if (i <= 0 && (!str1.empty()))
        cout << str1;
}
全部评论

相关推荐

TP-LINK 前端工程师 年包大概20出头 本科
点赞 评论 收藏
转发
点赞 评论 收藏
转发
点赞 收藏 评论
分享
牛客网
牛客企业服务