1. What is strtok?
strtok?strtok (string tokenize) is a C standard library function that splits a string into tokens based on delimiter characters.
Parameters:
str- String to tokenize (first call) orNULL(subsequent calls)delim- String containing delimiter characters
Returns:
- Pointer to the next token
NULLwhen no more tokens are found
2. Key Characteristic: Stateful Behavior
strtok is stateful - it remembers its position between calls using an internal static variable. This is why we use NULL in subsequent calls.
3. How It Works
3.1. The Two-Parameter Purpose
-
First parameter (str/NULL): WHERE to search
- Pass the string on first call to initialize
- Pass
NULLon subsequent calls to continue from saved position
-
Second parameter (delim): WHAT to search for
- Specifies which characters are delimiters
- Must be provided on every call (can be different each time)
3.2. What strtok Does
- Searches for the next delimiter character
- Replaces the delimiter with
'\0'(modifies original string!) - Returns pointer to the start of the current token
- Saves position internally for next call
- Returns
NULLwhen no more tokens exist
4. Step-by-Step Example
4.1. Input String
4.2. First Call - Initialize
What happens:
- Searches
strfor first',' - Replaces
','with'\0' - Returns pointer to
"James" - Saves internal position after the
'\0'
Memory after:
Result: name points to "James"
4.3. Second Call - Continue
What happens:
NULLmeans "use saved position"- Continues from saved position
- Searches for next
',' - Replaces it with
'\0' - Returns pointer to
"Hong Kong" - Updates saved position
Memory after:
Result: addr points to "Hong Kong"
4.4. Third Call - Continue
What happens:
- Continues from saved position
- No more
','found - Returns pointer to
"20" - Reaches end of string
Memory after:
Result: hours points to "20"
4.5. Fourth Call - End
Result: next is NULL (no more tokens)
5. Visual Flow Diagram
6. Complete Working Example
7. Why Use NULL in Subsequent Calls?
If we DON'T use NULL:
Passing the string again resets to the beginning.
With NULL (correct):
NULL tells strtok to continue from where it stopped.
8. Multiple Delimiter Characters
We can specify multiple delimiters in one string:
Common use case:
9. Using Different Delimiters Per Call
Each call can use different delimiters:
10. Loop Pattern
Common pattern to process all tokens:
11. Critical Warnings
11.1. Modifies Original String
strtok destroys the original string by replacing delimiters with '\0'.
Solution: Make a copy if we need the original
11.2. Not Thread-Safe
strtok uses a static internal variable, making it not thread-safe. Multiple threads calling strtok will interfere with each other.
Solution: Use strtok_r (reentrant version)
11.3. Cannot Use on String Literals
Solution: Use array instead
11.4. Empty Tokens
strtok skips consecutive delimiters:
If we need to preserve empty tokens, use alternative methods.
12. Common Use Cases
12.1. Parsing CSV Data
12.2. Parsing Command-Line Input
12.3. Splitting Path Components
13. Alternatives to strtok
13.1. strtok_r (Thread-Safe)
13.2. strsep (BSD/Linux)
13.3. Manual Parsing
14. Summary Table
| Feature | Behavior |
|---|---|
| First parameter | String pointer (first call) or NULL (subsequent calls) |
| Second parameter | Delimiter characters (required every call) |
| Modifies string | Yes - replaces delimiters with '\0' |
| Thread-safe | No - use strtok_r instead |
| Empty tokens | Skipped (consecutive delimiters ignored) |
| String literals | Cannot use - will crash |
| Returns | Pointer to token, or NULL when done |
15. Quick Reference
16. Key Takeaways
- First call: Pass the string to initialize
- Subsequent calls: Pass NULL to continue
- Delimiter: Can be different for each call
- Modifies string: Always makes a copy if we need the original
- Loop pattern: Common idiom is
while (token != NULL) - Thread safety: Use
strtok_rfor multithreaded code - NULL terminates: The function replaces delimiters with '\0'
- Stateful: Maintains internal position between calls











