题解 | #参数解析#

参数解析

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

#include<iostream>
#include<vector>
#include<string>
using namespace std;

int main(){
    string str, temp = "";
    vector<string> result;
    getline(cin, str);
    bool flag = true;
    int count = 0;
    for(int i = 0; i <= str.size(); i++) {
        //如果是空格或是最后一个字符,并且不是“”内的空格就保存起来
        if((str[i] == ' ' || i == str.size() )&& flag == true) {
            result.push_back(temp);
            temp.clear();
        } else if(str[i] != ' ' && str[i] != '\"') { // 不是空格,且不是“,就拼接起来
            temp += str[i];
        } else if(str[i] == ' ' && flag == false){// 是双引号内的空格也拼接起来
            temp += str[i];
        } else if(str[i] == '\"') { 
            count += 1;
            if(count % 2 == 1) { //遇到第一个双引号就不输出空格
                flag = false;
            }else if(count % 2 == 0) { // 第二个可以
                flag = true;
            }
        } 
    }
    cout << result.size() << endl;
    for(auto it = result.begin(); it != result.end(); it++)
        cout << *it << endl;
}

全部评论

相关推荐

点赞 收藏 评论
分享
牛客网
牛客企业服务