题解 | 参数解析

参数解析

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

定义一个bool变量 判断是否在引号里面

#include <iostream>

#include <vector>
using namespace std;

int main() {
    string command, tmp;
    vector<string> ans;
    bool in_quote = false;
    getline(cin, command);
    for(int i = 0; i < command.size(); ++i) {
        if(command[i] == '"') {
            in_quote = !in_quote;
            if(!in_quote) { //如果没有在引号里面 即此时是在引号的最右侧 直接push就行
                ans.push_back(tmp);
                tmp = "";
            }
        } else if(command[i] == ' ' && !in_quote) {
            if(!tmp.empty()) {
                ans.push_back(tmp);
                tmp = "";
            }
        } else tmp += command[i];
    }
    if(!tmp.empty()) {
        ans.push_back(tmp);
    }

    cout << ans.size() << endl;
    for(const string& s: ans) {
        cout << s << endl;
    }
    return 0;
}

全部评论

相关推荐

牛客928043833号:在他心里你已经是他的员工了
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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