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.
The general syntax for the strtok()
function is:
char *strtok(char *str, const char *delim)
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 doneThe function performs one split and returns a pointer to the token split up. A null pointer is returned if the string cannot be split.
Let’s start off by a simple example!
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.
#include<stdio.h>#include <string.h>int main() {char string[50] = "Hello world";// Extract the first tokenchar * token = strtok(string, " ");printf( " %s\n", token ); //printing the tokenreturn 0;}
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:
#include<stdio.h>#include <string.h>int main() {char string[50] = "Hello! We are learning about strtok";// Extract the first tokenchar * token = strtok(string, " ");// loop through the string to extract all other tokenswhile( token != NULL ) {printf( " %s\n", token ); //printing each tokentoken = strtok(NULL, " ");}return 0;}
Note: The
strtok()
function also modifies the original string. The original string is truncated to the first token.
#include<stdio.h>#include <string.h>int main() {char string[50] = "Hello world";printf( "Original string: %s\n", string ); //printing the string// Extract the first tokenchar * token = strtok(string, " ");printf( " %s\n", token ); //printing the tokenprintf( "String after strtok(): %s\n", string ); //printing the stringreturn 0;}
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
We use strtok()
in a loop in order to extract all tokens from a string.
What does the strtok()
function do in C?
Concatenates two strings
Searches for a substring within a string
Splits a string into smaller tokens based on a delimiter
Copies a string into another string
Ready to Master C Programming? Dive into our Learn C course and unlock everything from basic syntax to advanced debugging.
Free Resources