file-handling · beginner · ~15 min
Tokenise a delimited line by counting separators.
Count the comma-separated fields in a CSV line by counting the separators.
Implement int csv_field_count(const char *line) that returns the number of comma-separated fields in line.
line: a NUL-terminated string (one CSV row, no quoting).
Return the field count. An empty string has 0 fields; any non-empty string has (number of commas) + 1.
csv_field_count("a,b,c") -> 3
csv_field_count("x") -> 1
csv_field_count("") -> 0
csv_field_count("a,,b") -> 3 (the empty middle field counts)
line: a CSV row (no quoting).
int field count: 0 if empty, else commas + 1.
Count separators; empty fields still count.
int csv_field_count(const char *line) {
/* TODO */
return 0;
}
Solve this exercise in the browser editor — compile and run against the test harness, no setup required.