data-structures · intermediate · ~25 min

Final Project: CSV row parser (no quoting)

In-place tokenization without allocation; pointer-into-buffer arithmetic.

Challenge

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.

Why this matters

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.

Input format

Writable buffer. max_fields includes the NULL terminator slot? No — just the count of pointer slots.

Output format

Number of fields; fields[i] points into row.

Constraints

No malloc. Modifies row.

Starter code

int csv_parse_row(char *row, char **fields, int max_fields) { /* TODO */ return 0; }

Common mistakes

Allocating strdup'd copies (the API contract says split in place); not stripping trailing newline; off-by-one in max_fields.

Edge cases to handle

Empty row returns 1 with fields[0] = empty string. Trailing comma yields an empty last field.

Complexity

O(row length).

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