题解 | #参数解析#

参数解析

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

对字符串的操作,无非就是substr了,找到start 与 end 位置 就ok了,难度偏简单吧,偏基础
#include<iostream>
#include<vector>
#include<string>
#include<algorithm>
using namespace std;
string s1;
bool flag = true;
vector<string>ans;
int main() {
    getline(cin, s1);
    int start = 0;
    int end = 0;
    for (int i = 0; i < s1.size(); i++) {
        if (flag && (s1[i] == ' '||i==s1.size()-1)) {
            end = i;
            if (s1[start] == '"' && s1[end - 1] == '"')
                ans.push_back(s1.substr(start + 1, end - 1 - start - 1));
            else if (i == s1.size() - 1)
                ans.push_back(s1.substr(start, end - start + 1));
            else
                ans.push_back(s1.substr(start, end - start));
            start = end + 1;
            continue;
        }
        else if (flag && s1[i] != ' '&& s1[i] != '"') {
            continue;
        }
        else if (s1[i] == '"') {
            flag = !flag;
            if(i==s1.size()-1)
                ans.push_back(s1.substr(start+1, i - start-1));
            continue;
        }
        else
            continue;
    }
    int n = ans.size();
    cout << n << endl;
    for (auto c : ans)
    {
        cout << c << endl;
    }
    return 0;
}


全部评论

相关推荐

投递阿里巴巴控股集团等公司7个岗位 >
点赞 评论 收藏
转发
点赞 收藏 评论
分享
牛客网
牛客企业服务