What does strtok do? And how do we use it? I tried reading the man pages and tried some examples too but i m still unable to get the satisfactory explanation.
What does strtok do? And how do we use it? I tried reading the man pages and tried some examples too but i m still unable to get the satisfactory explanation.
EmbLogic Research & Competency Development Labs
Phone: +91 9818467776, 8527567776, 9650467776
Email: info@emblogic.com
Copyright © EmbLogic Embedded Technologies Pvt. Ltd.
strtok() will replace a the characters in the original string with a NULL each time the function is called using the same string, so the original string is modified by the use of strtok().
The second argument to strtok() can be changed at any time to a different delimiter.
Only single characters are considered to be delimiters.
The below example will illustrates the usage of strtok.
#include
#include
int main()
{
const char str[80] = “This is – http://www.emblogic.com – website”;
const char s[2] = “-”;
char *token;
/* get the first token */
token = strtok(str, s);
/* walk through other tokens */
while( token != NULL )
{
printf( ” %s\n”, token );
token = strtok(NULL, s);
}
return(0);
}
the output of the above program will be:
This is
http://www.tutorialspoint.com
website
The output will be:
This is
http://www.emblogic.com
website
and the first argument is not const char* but it is char*
Whenever we need to parse the same string , first argument in strtok() should be null.