题解 | #参数解析#

参数解析

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

#include <stdio.h>

int main() {
    char cmd[20][1000] = {0};
    int cmdCount = 0;
    int biao2Flag = 0;
    char ch = 0;
    int idx = 0;
    char lastch = 0;
    while(EOF != (ch = getchar()))
    {
        if(ch == '"')
        {
            if(biao2Flag == 0)
            {
                biao2Flag = 1;
                cmdCount++;
                idx = 0;
            }
            else //1
            {
                biao2Flag = 0;
            }
        }
        else if(' ' == ch)
        {
            if(1 == biao2Flag)
                cmd[cmdCount][idx++] = ch;
            else if(0 == biao2Flag)
            {
                lastch = ch;
            }
        }
        else //ch is char
        {
            if(lastch == 0)
            {
                cmd[cmdCount][idx++] = ch;
                lastch = ch;
            }
            else if((lastch == ' ' && 0 == biao2Flag))
            {
                cmdCount++;
                idx = 0;
                cmd[cmdCount][idx++] = ch;
                lastch = ch;
            }
            else 
            {
                cmd[cmdCount][idx++] = ch;
                lastch = ch;
            }
        }
    }
    printf("%d\n",cmdCount+1);
    for(int i = 0; i <= cmdCount; i++)
    {
        printf("%s\n",cmd[i]);
    }

    return 0;
}

全部评论

相关推荐

点赞 评论 收藏
分享
shtdbb_:还不错,没有让你做了笔试再挂你
点赞 评论 收藏
分享
评论
点赞
收藏
分享
牛客网
牛客企业服务