C - 拆分字符串

C - split string

如果我想把它变成 4 个单词,我该如何处理这个字符串。如果我使用 sscanf,它会将单词 'Face masks' 一分为二。或者可以以某种方式使用 sscanf 来防止这种情况发生吗?

输入:

2021-01-01 2021-7-1 'Face masks' "Wear everywhere"
2000-08-05 2010-8-8 LOCKDOWN 'xxxxx'

输出:

2021-01-01
2021-7-1 
'Face masks' 
"Wear everywhere"
....

scanf() 对于您的目的来说不够强大,因为可选的引号和可能的空词(''"")。

这是一个通用的手工编码解析器:

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

// split a string into up to count words stored in a 2D array of char
int split(char output[][100], int count, const char *str) {
    // recognises space separated words and quoted content without
    // embedded quotes of the same type. Quotes are stripped in output.
    for (int i = 0; i < count; i++) {
        const char *start;
        int len;

        str += strspn(str, " \t\f\v\r\n");
        if (*str == '[=10=]')
            return i;
        if (*str == '\'') {
            start = ++str;
            len = strcspn(str, "\'");
            str += len;
            str += (*str == '\'');
        } else
        if (*str == '\"') {
            start = ++str;
            len = strcspn(str, "\"");
            str += len;
            str += (*str == '\"');
        } else {
            start = str;
            len = strcspn(str, " \t\f\v\r\n");
            str += len;
        }
        snprintf(output[i], sizeof(output[i]), "%.*s", len, start);
    }
}