Please explain the Function that is in C++. The Function is strtok(); Please explain this function with the help of example.

         Imran Ahmad Mughal

Hi Imran.
In the beginning I have given technical details.Explanation is towards the end.

strtok : <string.h>   

char *  strtok ( const char * string, const char * delimiters );

Sequentially truncate string if delimiter is found.
If string is not NULL, the function scans string for the first occurrence of any character included in delimiters. If it is found, the function overwrites the delimiter in string by a null-character and returns a pointer to the token, i.e. the part of the scanned string previous to the delimiter.
After a first call to strtok, the function may be called with NULL as string parameter, and it will follow by where the last call to strtok found a delimiter.
delimiters may vary from a call to another.

Return Value.
 A pointer to the last token found in string.   NULL is returned when there are no more tokens to be found.


/* strtok example */
#include <stdio.h>
#include <string.h>

int main ()
 char str[] ="This is a sample string,just testing.";
 char * pch;
 printf ("Splitting string \"%s\" in tokens:\n",str);
 pch = strtok (str," ");
 while (pch != NULL)
   printf ("%s\n",pch);
   pch = strtok (NULL, " ,.");
 return 0;

Splitting string "This is a sample string,just testing." in tokens:

The real job of strtok() is to scan your string for delimeters that you specify in the function call. Once I explain the above example, you will be clear with the concept.
I have created a string str[]. I also have a char pointer pch. I have used strtok 2 times.First time, I say this:pch=strtok(str," ");
that is "scan string str for " "(blank space), and when found, return all that is between start of scan and end of scan. Also truncate str to contain only this character"

Then in my while loop, when I first say printf, it prints pch, which is "This". It is so because this comes just before the first " ".

Then in my while loop, I say pch=strtok(NULL," ,.");
That is,"scan str for space,comma or full stop but start scanning from where it was stopped last time, and return to pch what is between the start of scan and the end of scan"

We use "NULL" here so that scan may start from where it stopped last time, otherwise if we use str it will start from the beginning and will keep printing "This" endlessly.

So,the string is scanned starting just after the first space after this(thats where we stopped scanning last time) till we find a space(after "is"). Then the while loop runs again and "is" is printed. Notice that "This is" is not printed because we started scan after "This " and ended it before "a sample ...". So only "is" is displayed. This continues till a scan results in nothing being found(at the end of string after full stop) and hence NULL is returned and program ends.

As you cansee, strtok is printing parts of string enclosed between the delimeters you specify.
It is a little confusing.Try out the example yourself and experiment a little with strtok. Change delimetres in second strtok from " ,." to ",."
that is remove the space, and then see the output.Make changes ahain and check. You will get the idea then.


All Answers

Answers by Expert:

Ask Experts


Samarth Bartaria


I can answer questions about pointers behaviour, their implementations and anamolous behaviour.Also, I speciallize in object oriented design and modelling,polymorphism in C++ and algorithm efficiency. Even questions related to database design,or simple basics about programming are welcome.


I have been using C++ for five years now for software development and scientific analyses.

Currently, I am a student doing my computer engineering.

©2017 About.com. All rights reserved.

[an error occurred while processing this directive]