题解 | #参数解析#

参数解析

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

#include <stdio.h>
#include <string.h>

int main() {
    char s[1001];
    while (fgets(s, sizeof(s), stdin) != NULL) {
        int len = strlen(s);
        char argument[500][100];
        int argc = 0;
        int inQuote = 0;
        char* temp = NULL;
        int tempIndex = 0;
        for (int i = 0; i < len; i++) {
            if (!inQuote && (s[i] != '"' && s[i] != ' ')) {
                temp = argument[argc];
                tempIndex = 0;
                while (i < len && s[i] != '"' && s[i] != ' ') {
                    temp[tempIndex++] = s[i++];
                }
                temp[tempIndex] = '\0';
                argc++;
            } else if (s[i] == '"') {
                temp = argument[argc];
                tempIndex = 0;
                i++;
                while(s[i] != '"' && i < len) {
                    temp[tempIndex++] = s[i++];
                }
                temp[tempIndex] = '\0';
                argc++;
                i++;
            } else if (s[i] == ' ') {
                continue;
            }
        }
        printf("%d\n", argc);
        for (int i = 0; i < argc; i++) {
            printf("%s\n", argument[i]);
        }
    }
    return 0;
}

全部评论

相关推荐

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