data-structures · intermediate · ~25 min
In-place tokenization without allocation; pointer-into-buffer arithmetic.
Implement int csv_parse_row(char *row, char **fields, int max_fields). Splits row on commas in place — replaces each , with \0 — and stores pointers to each field in fields[]. Returns the number of fields (always >= 1 for non-empty input).
The input is one logical CSV row, may contain a trailing \n (strip it). No quoted fields.
CSV is the eternal interchange format. A simple, non-quoting parser is enough for >80% of real CSV files and is a great exercise in stateless splitting.
Writable buffer. max_fields includes the NULL terminator slot? No — just the count of pointer slots.
Number of fields; fields[i] points into row.
No malloc. Modifies row.
int csv_parse_row(char *row, char **fields, int max_fields) { /* TODO */ return 0; }
Allocating strdup'd copies (the API contract says split in place); not stripping trailing newline; off-by-one in max_fields.
Empty row returns 1 with fields[0] = empty string. Trailing comma yields an empty last field.
O(row length).
Solve this exercise in the browser editor — compile and run against the test harness, no setup required.