file-handling · intermediate · ~15 min

Extract a CSV field

Walk delimited text to a specific field.

Challenge

Implement int csv_get_field(const char *line, int idx, char *out, int outsz) copying the 0-based field idx into out (NUL-terminated, bounded by outsz) and returning its length, or -1 if idx is out of range.

Starter code

#include <string.h>

int csv_get_field(const char *line, int idx, char *out, int outsz) {
    /* TODO */
    return -1;
}

Solve this exercise in the browser editor — compile and run against the test harness, no setup required.