Explore strtok() Function in C

Explore strtok() Function in C

4 mins read429 Views Comment
Updated on Mar 31, 2023 09:13 IST

The strtok() function in C is a useful tool for splitting strings into tokens based on a delimiter. With its various parameters, strtok() can help you efficiently parse and manipulate strings in your C code. Learn more about using strtok() to work with strings in C.

2023_03_Strtok-function-in-C.jpg

We will discuss the strtok() function in C programming language that divides a given string into a sequence of tokens using the specified delimiter as an argument.

Introduction to strtok() Function in C

The strtok() function is a library function in the C programming language that breaks a given string into a series of tokens using a specified delimiter. This function is declared in the string.h header file and is commonly used for text parsing and manipulation operations.

Here is the syntax for the strtok() function:


 
char *strtok(char *str, const char *delim);
Copy code

Parameters of strtok()

The strtok function in C takes two arguments: 

  • str – a pointer to the string to tokenize. This parameter should be NULL on all subsequent calls to strtok() for the same string.
  • delim – a pointer to a null-terminated string of delimiter characters. The delimiters specify the points at which the string to split into tokens.

When strtok() is called for the first time, the pointer to the string is passed as the first argument, and subsequent calls to strtok() use NULL as the first argument to continue tokenizing the same string.

Return Value of strtok()

The function returns a pointer to the first token found in the input string str, or NULL if no tokens were found.

Note that the strtok() function modifies the original input string by replacing each delimiter with a null character to terminate each token. Therefore, the original string should not be modified or freed until all tokens have been processed.

Learn to Implement strstr() Function in C
Learn to Implement strstr() Function in C
Discover how to use the Strstr function in the C programming language, including its syntax, parameters, and practical applications. Explore examples and tips to effectively implement Strstr in your code...read more
Concatenate Strings with strcat() Function in C
Concatenate Strings with strcat() Function in C
This article offers a comprehensive explanation of the syntax and implementation of the strcat() function, in addition to providing several examples to aid in understanding its operation. By the end...read more
Compare Strings with strcmp Function in C
Compare Strings with strcmp Function in C
The strcmp function in C is used to compare two strings and determine whether they are equal or not. This article explains the syntax and usage of the strcmp function...read more

C Examples of strtok() Function 

Example 1: Tokenizing a string with a comma delimiter


 
#include <stdio.h>
#include <string.h>
int main() {
char str[] = "apple,banana,cherry,orange";
char *token = strtok(str, ",");
while (token != NULL) {
printf("%s\n", token);
token = strtok(NULL, ",");
}
return 0;
}
Copy code

Output:

apple

banana

cherry

orange

In this example, we call strtok function in C for the first time with str as the first argument and “,” as the second argument. This will find the first token in the string, which is “apple”. We then print the token using printf() and call strtok() again with NULL as the first argument and “,” as the second argument to find the next token. This process repeats until all tokens extract, at which point strtok() returns NULL to indicate no more tokens are there.

The output of the program is the four tokens that extract from the original string: “apple”, “banana”, “cherry”, and “orange”. Each token prints on a new line using printf().

Let’s look at other similar examples:

Example 2: Tokenizing a string with a space delimiter


 
#include <stdio.h>
#include <string.h>
int main() {
char str[] = "The quick brown fox";
char *token = strtok(str, " ");
while (token != NULL) {
printf("%s\n", token);
token = strtok(NULL, " ");
}
return 0;
}
Copy code

Output:

The

quick

brown

fox

Example 3: Tokenizing a string with multiple delimiters


 
#include <stdio.h>
#include <string.h>
int main() {
char str[] = "1,2;3-4";
char *token = strtok(str, ",;-");
while (token != NULL) {
printf("%s\n", token);
token = strtok(NULL, ",;-");
}
return 0;
}
Copy code

Output:

1

2

3

4

In this example, the user is prompted to enter a sentence. The strtok() function [insert link] tokenises the sentence by space. The strrev() function then reverses each token (word) in the sentence. Finally, the reversed sentence is printed on the screen.

Example 4: Using the strtok() function in a practical application to split a string into its individual components

Suppose you have a string that contains data separated by commas, like this:


 
char data[] = "John,Smith,32,Male";
Copy code

You want to extract each component of the data (first name, last name, age, gender) and store them in separate variables. You can do this using the strtok() function to split the string into tokens:


 
#include <stdio.h>
#include <string.h>
int main() {
char data[] = "John,Smith,32,Male";
char *token;
char *delim = ",";
char firstname[20], lastname[20], age[3], gender[10];
// extract the first name
token = strtok(data, delim);
strcpy(firstname, token);
// extract the last name
token = strtok(NULL, delim);
strcpy(lastname, token);
// extract the age
token = strtok(NULL, delim);
strcpy(age, token);
// extract the gender
token = strtok(NULL, delim);
strcpy(gender, token);
// print out the extracted data
printf("First name: %s\n", firstname);
printf("Last name: %s\n", lastname);
printf("Age: %s\n", age);
printf("Gender: %s\n", gender);
return 0;
}
Copy code

Output:

First name: John

Last name: Smith

Age: 32

Gender: Male

In this example, we first declare a character array data that contains a comma-separated string of four values: “John,Smith,32,Male”. We then declare a character pointer token, a string delim that specifies the delimiter character (“,”), and four character arrays firstname, lastname, age, and gender to store the extracted values.

  • The first call to strtok() extracts the first token from data, which is “John”. It copies into the firstname array using the strcpy() function.
  • The next call to strtok() extracts the second token from data, which is “Smith”. It copies into the lastname array using strcpy().
  • The third call to strtok() extracts the third token from data, which is “32”. It copies into the age array using strcpy().
  • The fourth and final call to strtok() extracts the fourth token from data, which is “Male”. This goes into the gender array using strcpy().

Finally, the extracted values print out using printf() statements.

Endnotes

In conclusion, the strtok function in C is a powerful tool for splitting strings into substrings based on a given delimiter. It is applicable for parsing data from input streams, tokenizing text files, and extracting information from command-line arguments.

Check out C programming courses to get the best certifications from top vendors.

Contributed By Prerna Singh

About the Author

This is a collection of insightful articles from domain experts in the fields of Cloud Computing, DevOps, AWS, Data Science, Machine Learning, AI, and Natural Language Processing. The range of topics caters to upski... Read Full Bio