Trying to understand strtok
Posted
by
Karthick
on Stack Overflow
See other posts from Stack Overflow
or by Karthick
Published on 2011-01-14T02:26:18Z
Indexed on
2011/01/14
2:53 UTC
Read the original article
Hit count: 323
Consider the following snippet that uses strtok to split the string madddy.
char* str = (char*) malloc(sizeof("Madddy"));
strcpy(str,"Madddy");
char* tmp = strtok(str,"d");
std::cout<<tmp;
do
{
std::cout<<tmp;
tmp=strtok(NULL, "dddy");
}while(tmp!=NULL);
It works fine, the output is Ma. But by modifying the strtok to the following,
tmp=strtok(NULL, "ay");
The output becomes Madd. So how does strtok exactly work? I have this question because I expected strtok to take each and every character that is in the delimiter string to be taken as a delimiter. But in certain cases it is doing that way but in few cases, it is giving unexpected results. Could anyone help me understand this?
© Stack Overflow or respective owner