题解 | #参数解析#

参数解析

https://www.nowcoder.com/practice/668603dc307e4ef4bb07bcd0615ea677

#include <iostream>
using namespace std;
#include <string>
#include <vector>
#include <sstream>
int main() {
    string in;
    getline(cin, in);
    istringstream is(in);
    string word;
    vector<string> ws;
    while (is >> word) {
        if (word[0] == '"') {
            if (word[word.size() - 1] == '"') {
                word.erase(word.begin());
                word.erase(word.end()-1);
                ws.push_back(word);
            } else {
                string str;
                word.erase(word.begin());
                str += word;
                do {
                    is >> word;
                    str = str + " " + word;
                } while (word[word.size() - 1] != '"');
                str.erase(str.end()-1);
                ws.push_back(str);
            }
        } else {
            ws.push_back(word);
        }
    }
    cout << ws.size() << endl;
    for(auto & w : ws){
        cout << w << endl;
    }
}

比较标准的输入处理

要记得去除头尾的引号

华为机试刷题记录 文章被收录于专栏

记录一下手打代码的解题思路方便复习

全部评论

相关推荐

牛客ID:561366855:期望薪资多少?难以相信这简历找不到工作。说明二本电子信息专业想对口就业非常难。
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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