题解 | #参数解析#

参数解析

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

#include<iostream>
#include<vector>
using namespace std;
//先检查引号,遇到奇数个引号的时候进入引号内,直到下一个引号的长度解析为一个参数
//再对空格进行判断,如果空格是在引号外,则空格分割参数
vector<string> parameter_analysis(string s){
    vector<string> res;
    string str="";
    bool flag=false;
    int n=s.length();
    for(int i=0;i<n;i++){
      if(s[i]=='\"'){
          if(flag==false){
              flag=true;
          }else{
              flag=false;
              res.push_back(str);
              str.clear();
          }
      }else{
          if(flag==true){
              str+=s[i];
          }else{
              if((s[i]==' ')&&(str.empty()==false)){
                res.push_back(str);
                str.clear();
              }
              else if(s[i]!=' '){
                    str+=s[i];
              }
          }
      }
    }
    if(str.empty()==false){
        res.push_back(str);
    }
    return res;
}
int main(){
    string temp;
    while(getline(cin,temp)){
        vector<string> str=parameter_analysis(temp);
        int m=str.size();
        cout<<m<<endl;
        for(int i=0;i<m;i++){
            cout<<str[i]<<endl;
        }
    }
    return 0;
}
全部评论

相关推荐

程序员牛肉:这一眼假啊,基本上都是骗人的,不然就涉及到职位贪腐了,就像之前华为的OD事件,看你运气好不好了
点赞 评论 收藏
分享
不愿透露姓名的神秘牛友
06-05 04:14
已编辑
真烦好烦真烦:看着感觉好强啊,这都过不了吗
投递字节跳动等公司7个岗位 面试中的破防瞬间
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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