Splitting a string using strtok() in C
In C, the strtok() function is used to split a string into a series of tokens based on a particular delimiter. A token is a substring extracted from the original string.
Syntax
The general syntax for the strtok() function is:
Parameters
Let’s look at the parameters the strtok() function takes as input:
str: The string which is to be splitdelim: The character on the basis of which the split will be done
Return value
The function performs one split and returns a pointer to the token split up. A null pointer is returned if the string cannot be split.
Example
Let’s start off by a simple example!
1. Extracting a single token
The following piece of code will just split up the Hello world string in two and will return the first token extracted from the function.
2. Extracting all possible tokens
To find all possible splits of the string, based on a given delimiter, the function needs to be called in a loop. See the example below to see how this works.
Let’s see how we can split up a sentence on each occurrence of the white space character:
Note: The
strtok()function also modifies the original string. The original string is truncated to the first token.
Characteristics and limitations of strtok()
The input string is modified by replacing the delimiter with null characters. This is not a good thing when we want to preserve the original string.
It is not
.thread-safe Thread-safe means that a piece of code or function can be safely used by multiple threads concurrently without causing unexpected behavior or data corruption. We use
strtok()in a loop in order to extract all tokens from a string.
Test your knowledge !
Ready to Master C Programming? Dive into our Learn C course and unlock everything from basic syntax to advanced debugging.
Free Resources