C in 100 Seconds: String Tokenizing with strtok
Video: C in 100 Seconds: String Tokenizing — strtok | Episode 33 by Taught by Celeste AI - AI Coding Coach
Watch full page →String Tokenizing with strtok
strtok splits a string into tokens based on delimiters. It modifies the original string by replacing delimiters with null bytes.
Basic Usage — Split by Spaces
char sentence[] = "The quick brown fox";
char *word = strtok(sentence, " ");
while (word != NULL) {
printf("%s\n", word);
word = strtok(NULL, " ");
}
The first call passes the string. Every call after that passes NULL to continue tokenizing the same string.
Parsing CSV Data
char csv[] = "Alice,30,Engineer";
char *field = strtok(csv, ",");
while (field != NULL) {
printf("Field: %s\n", field);
field = strtok(NULL, ",");
}
Same pattern, just change the delimiter from space to comma.
Multiple Delimiters
char data[] = "red;green,blue:yellow";
char *color = strtok(data, ";,:");
Pass all delimiters in a single string — strtok splits on any of them.
Important Notes
strtokmodifies the original string (replaces delimiters with\0)- It uses internal state, so it's not thread-safe
- For thread-safe tokenizing, use
strtok_r
Student Code
Try it yourself: episode33/tokenize.c