How to split a string on commas, ignoring the commas in quoted substrings with C

1 Answer

0 votes
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

char **split_string(const char *str, int *count) {
    char **result = NULL;
    int capacity = 0;
    *count = 0;

    int in_quotes = 0;
    char buffer[1024] = "";
    int buf_i = 0;

    for (int i = 0; ; i++) {
        char c = str[i];

        if (c == '"') {
            in_quotes = !in_quotes;
            buffer[buf_i++] = c;
        }
        else if (c == ',' && !in_quotes) {
            buffer[buf_i] = '\0';

            if (*count >= capacity) {
                capacity = capacity == 0 ? 4 : capacity * 2;
                result = realloc(result, capacity * sizeof(char *));
            }

            result[*count] = strdup(buffer);
            (*count)++;

            buf_i = 0;
        }
        else if (c == '\0') {
            buffer[buf_i] = '\0';

            if (*count >= capacity) {
                capacity = capacity == 0 ? 4 : capacity * 2;
                result = realloc(result, capacity * sizeof(char *));
            }

            result[*count] = strdup(buffer);
            (*count)++;
            break;
        }
        else {
            buffer[buf_i++] = c;
        }
    }

    return result;
}

int main() {
    const char *input = "one,\"two, with comma\",three,\"four, with \"\"quotes\"\"\"";

    int count;
    char **fields = split_string(input, &count);

    for (int i = 0; i < count; i++) {
        printf("Split %d: [%s]\n", i, fields[i]);
        free(fields[i]);
    }

    free(fields);
    
    return 0;
}


/*
run:

Split 0: [one]
Split 1: ["two, with comma"]
Split 2: [three]
Split 3: ["four, with ""quotes"""]

*/

 



answered May 7 by avibootz
edited May 7 by avibootz
...