题解 | #参数解析#

参数解析

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

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

int main() {
    string a;
    vector<string> data;
    while(getline(cin,a))
    {
        int p = 0;
        bool flag = false; //记录是否进入引号中
        for(int i = 0; i < a.length(); i++)
        {
            if(a[i] == '"')
            { //遇到引号
                if(!flag) //第一个引号
                    flag = true;
                else
                { //第二个引号
                    flag = false;
                    data.push_back(a.substr(p, i - p)); //截取字符串加入
                }
                p = i + 1;
            } 
            else if(a[i] == ' ' && !flag)
            { //遇到引号外的空格
                if(i != p)  //非空字符串
                    data.push_back(a.substr(p, i - p)); //截取字符串加入
                p = i + 1;
            } 
            else if(i == a.length() - 1) //最后一个参数字符串
                data.push_back(a.substr(p, i - p + 1));
        }
    }

    cout << data.size() << endl;

    for (auto it : data)
    {
        cout << it;
        cout << endl;
    } 
}
// 64 位输出请用 printf("%lld")

全部评论

相关推荐

04-19 11:59
门头沟学院 Java
卷不动辣24314:挂,看来不该投这个部门的
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务