题解 | #句子逆序#

句子逆序

http://www.nowcoder.com/practice/48b3cb4e3c694d9da5526e6255bb73c3

#include <iostream>
#include <fstream>
#include <string>
#include <algorithm>
#include <vector>

using namespace std;

void process(string str, string& res){
    string tmp = "";
    vector<string> vec;
    
    int idx = 0;
    while(str[idx] == ' '){
        idx++;
    }
    str = str.substr(idx);
    str += ' '; //补一个空格,避免最后一个单词无法取出
    
    for(char c : str){
        if(c == ' '){
            if(!tmp.empty()){            
                vec.push_back(tmp);
                tmp.clear();
            }
        }
        else{
            tmp += c;//cout<<tmp<<endl;
        }
    }
    
    reverse(vec.begin(), vec.end());

    for(string s : vec){
        res += s;
        res += " ";
    }
    
    res.pop_back(); //把最后一个空格推出去
}

int main(){
    string str = "";
    getline(cin, str);
    
    //cout<<str<<endl;
    string res = "";
    process(str, res);
    cout<<res<<endl;
    
    return 0;
}
华为题库题解 文章被收录于专栏

牛客华为题库的题解

全部评论

相关推荐

darius_:我试了简历上有微服务和没微服务主动要简历的外包的比例都不一样,微服务稍微看看还是要写上去,人人都写你不写会被pass
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务