strtok is recommended for parsing a string; I don't care for
strtok . Why?
- It modifies the incoming string, so it cannot be used with string literals or other constant strings.
- The identity of the delimiting character is lost.
- It uses a static buffer while parsing, so it's not reentrant.
- It does not correctly handle "empty" fields -- that is, where two delimiters are back-to-back and meant to denote the lack of information in that field.
This snippet shows a way to use
strcspn to parse a string into fields delimited by a character (a semicolon in this case, but commas or tabs or others could be used as well).
Thanks to dwks for asking why not to use strtok.