Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Last Updated: Mar 27, 2024
Difficulty: Easy

strtok() in C

Leveraging ChatGPT - GenAI as a Microsoft Data Expert
Speaker
Prerita Agarwal
Data Specialist @
23 Jul, 2024 @ 01:30 PM

Introduction

When it comes to handling and manipulating strings in the C programming language, the standard library provides several useful functions. One of these is the strtok() function. It's a powerful yet sometimes misunderstood function used to split a string into tokens or smaller segments. 

strtok() in C

This article will shed light on the strtok() function, diving deep into its syntax, usage, and peculiarities.

A Look at Syntax

The syntax of the strtok() function is as follows:

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

Here, str refers to the string that needs to be separated into tokens, and delim is the delimiter string containing all possible delimiters.

Understanding strtok()

The strtok() function breaks down the string into smaller tokens, which can be accessed one at a time. The function uses a static pointer internally to keep track of the next token to be returned. Initially, the str pointer is passed, and subsequently, a NULL pointer is passed to continue tokenizing the same string. Here's a simple demonstration:

#include <stdio.h>
#include <string.h>


int main() {
   char str[100] = "Hello, how are you?";
   char *token = strtok(str, " ,");
   
   while(token != NULL) {
      printf("%s\n", token);
      token = strtok(NULL, " ,");
   }
   
   return 0;
}

Output

otuput

In this example, the string "Hello, how are you?" is broken into tokens by space and comma delimiters. The tokens are then printed one by one.

Also read - Bit stuffing program in c


Things to Note

While strtok() is a valuable tool, there are few things you should bear in mind:
 

  • The strtok() function modifies the string it is tokenizing. It places a \0 character at the location of each delimiter to break up the string.
     
  • It's not thread-safe because it uses a static pointer internally. For a thread-safe version, you might want to consider using strtok_r().
     
  • It cannot handle multiple delimiters in a row, or tokens that begin or end with delimiter characters.

and Floyd's Triangle in C

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

Frequently Asked Questions

What does strtok() do in C?

The strtok() function in C breaks a string into a series of tokens using specified delimiters.

Is strtok() a safe function to use in C?

strtok() is not thread-safe due to its use of a static pointer. In multi-threaded environments, consider using strtok_r().

Can strtok() handle multiple delimiters?

Yes, strtok() can handle multiple delimiters, but not multiple consecutive delimiters. It treats them as a single delimiter.

Conclusion

The strtok() function, while powerful, has its share of idiosyncrasies that programmers should be aware of. It's an excellent tool for breaking a string into smaller tokens, but its non-thread-safe nature and inability to handle multiple consecutive delimiters limit its applicability. By understanding its working mechanism and limitations, programmers can make informed decisions about when and how to use strtok() effectively.

Check out these useful blogs on - 


Check out the following problems - 

 

Refer to our guided paths on Coding Ninjas Studio to learn more about DSA, Competitive Programming, JavaScript, System Design, etc. Enroll in our courses and refer to the mock test and problems available, interview puzzles, take a look at the interview experiences, and interview bundle for placement preparations.

Do upvote our blog to help other ninjas grow

Happy Reading!!‍

Topics covered
1.
Introduction
2.
A Look at Syntax
2.1.
Understanding strtok()
2.2.
Things to Note
3.
Frequently Asked Questions
3.1.
What does strtok() do in C?
3.2.
Is strtok() a safe function to use in C?
3.3.
Can strtok() handle multiple delimiters?
4.
Conclusion