You are given a single line of CSV-formatted text. Fields are separated by commas, following these rules:
")."").The line is guaranteed to be validly formatted per these rules (every quoted field's quotes are properly opened and closed). Count how many fields the line has. A line with no commas at all (quoted or not) still has exactly one field; a trailing comma implies a trailing empty field.
Line 1: the CSV line (may be empty, which counts as one field containing the empty string).
A single integer: the number of fields.
Example 1
Input
apple,banana,cherry
Expected
3
Explanation
Three unquoted fields separated by commas, so the count is 3.
Example 2
Input
"a,b",c
Expected
2
Explanation
The quoted field `"a,b"` contains a comma that is not a separator, so there are 2 fields total: `a,b` and `c`.
Ready to solve this?
Sign in to open the editor, run your code against the sample tests, and submit against the full test suite.
Sign in to solve →