Trusted answers to developer questions

Splitting a string using strtok() in C

Free System Design Interview Course

Many candidates are rejected or down-leveled due to poor performance in their System Design Interview. Stand out in System Design Interviews and get hired in 2024 with this popular free course.

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.

How strtok( ) works
How strtok( ) works

Syntax

The general syntax for the strtok() function is:

char *strtok(char *str, const char *delim)

Parameters

Let’s look at the parameters the strtok() function takes as input:

  • str: The string which is to be split
  • delim: 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.

#include<stdio.h>
#include <string.h>
int main() {
char string[50] = "Hello world";
// Extract the first token
char * token = strtok(string, " ");
printf( " %s\n", token ); //printing the token
return 0;
}

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:

#include<stdio.h>
#include <string.h>
int main() {
char string[50] = "Hello! We are learning about strtok";
// Extract the first token
char * token = strtok(string, " ");
// loop through the string to extract all other tokens
while( token != NULL ) {
printf( " %s\n", token ); //printing each token
token = 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 token
char * token = strtok(string, " ");
printf( " %s\n", token ); //printing the token
printf( "String after strtok(): %s\n", string ); //printing the string
return 0;
}

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-safeThread-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 !

1

What does the strtok() function do in C?

A)

Concatenates two strings

B)

Searches for a substring within a string

C)

Splits a string into smaller tokens based on a delimiter

D)

Copies a string into another string

Question 1 of 30 attempted

RELATED TAGS

c
string split
strtok
Copyright ©2024 Educative, Inc. All rights reserved
Did you find this helpful?