Bifalgorithm post · Computer science one · Tokenizing
How can strtok() split a string into tokens?
How can strtok() split a string into tokens? (Computer science one · Tokenizing) — "Recall that tokenizing is the process of splitting up a string along some d…
Ideas in this flowchart
- Tokenizing strings with strtok()
- Tokenizing splits a string along a delimiter.
- The comma-delimited string "Smith,Joe,12345678,1985-09-08" contains four data pieces.
- The goal is to produce separate strings so each piece can be processed.
- The C string library provides strtok(char *str, const char *delim).
- The first argument is the string to tokenize, and the second specifies the delimiter.
- The delimiter argument is a string, although this example uses single-character delimiters.
- strtok() returns a pointer to the first token and returns NULL when no tokens remain.
- Passing NULL continues parsing the same string; passing a new string starts tokenization over.
- strtok() modifies the input string, so a deep copy is needed when the original must be preserved.
- A while loop can repeatedly process tokens until strtok() returns NULL.
- The example initializes strtok(data, ",") and then calls strtok(NULL, ",") for subsequent tokens.
- The example outputs Smith, Joe, 12345678, and 1985-09-08 as separate tokens.