strtok() in C

Learn via video course
FREE
View all courses
C++ Course: Learn the Essentials
C++ Course: Learn the Essentials
by Prateek Narang
1000
5
Start Learning
C++ Course: Learn the Essentials
C++ Course: Learn the Essentials
by Prateek Narang
1000
5
Start Learning
Topics Covered

Overview

The strtok in c is a library function which breaks the given string into a series of tokens using the delimiter passed as a parameter. Like if there is a string with multiple words separated by comma then we can use this function strtok in c with comma(,) as a delimiter to split this string based on comma. The delimiter can be anything like a number,character, space or special characters.

Build an AI-First Career, Master the Complete Skillset

Choose from our industry-leading programs designed for career success

NSDC Certified

Modern Software and AI Engineering Program

Master full-stack development with AI integration

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Modern Data Science and ML with specialisation in AI

Advanced data science techniques with AI specialization

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Advanced AIML with Specialisation in Agentic AI

Deep dive into AIML with focus on Agentic systems

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

DevOps, Cloud & AI Platform Engineering

Build and manage AI-powered cloud infrastructure

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

AI Engineering Advanced Certification by IIT-Roorkee

Premier AI engineering certification from IIT-Roorkee

3 MonthsDuration
AI-LedCurriculum
Career SupportSupport
Program highlights
Go to Program
NSDC Certified

AI Forward Deployed Engineer Program

Full-stack engineering, production AI and client-facing consulting

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program

Syntax of strtok() in C

The syntax of function strtok in c Language is given below:

Parameters of strtok() in C

string − This is the string which is passed as the parameter which is to be modified by this function.

delimiter − This is the string containing the delimiter on the basis of which string will be broken into series of tokens.And these may vary from one call to another.

Sharpen Your Fundamentals with Free Learning

Return Value of strtok() in C

The return value of this function is a pointer to the first token which is found in the string on breaking it into several tokens. And if there will be no tokens left to retrieve, a null pointer is returned.

How Scaler Transformed Careers in Different Fields

₹23L
AVG CTC
SCALER PLACEMENT PROOF

Scaler learners achieved 2.5x salary growth with average post-Scaler CTC reaching ₹23L.

11,000+placements
650+companies
Verified data
Hiring Partners:
GoogleGoogleAmazonAmazonMicrosoftMicrosoftFlipkartFlipkartAdobeAdobe1200+ more

Example

A program which splits a string using function strtok in c.

**Output of this program would be - **

Exceptions of strtok() in C

There are not any exceptions of function strtok in c and it works the same way for all cases. If the delimiter is not present in the string , then it returns NULL as the token.

Turn Learning into Career Growth

1200+Hiring Partners
89%Placement Rate
11,000+Placements
147%Avg Salary Increment
2.5XCareer Growth
₹23 LPAAvg Post-Scaler Salary
1200+Hiring Partners
89%Placement Rate
11,000+Placements
147%Avg Salary Increment
2.5XCareer Growth
₹23 LPAAvg Post-Scaler Salary

What is strtok() in C?

strtok() is a function in C language which take a string and a delimeter then breaks the given string into a series of tokens using the delimiter passed as a parameter. It then return pointer to the first token which is found in the string on breaking it into several tokens.

More Examples

Code of function strtok in c to describe practical application of the function using splitting the string.

Output of this program would be -

Conclusion

  • strtok in c is used to split a string in multiple strings on the basis of separators or delimiters. We can also implement CSV file using this strtok() function as CSV files are separated by commas which is a delimiter in that function.
  • This function can be used to split any string or multiple strings based on the delimiter provided.
  • #include <string.h> This header file should always be included when we run the program consisting of strtok() function.
  • The delimiter can be anything like alphabet,numeric characters,special characters etc.
  • We can also import a csv file and apply this function to get the required result by splitting the file based on the delimiter.