We help IT Professionals succeed at work.

strtok and why it works

How come in all my c++ books, the explanation of strtok and it's arguments is as follows:

"The first call to strtok contains two arguments... The second argument, " ", indicates that tokens ... are separated by spaces."

Now why does it work if you include something like this:
tokenPtr = strtok(string, ",, ;, :, !, ?, \t, \n, -,  ") where you still technically have only 1 argument, but there is a lot more than a space in between " ".

Isn't that cheating sort of? Who ever found out that it works, and why does it work???????

Just curious so please help me out here. Thanks.
Watch Question

No, it's not cheating.
It simply scans your string for the characters designated as separators and replaces them withe NULL.
The second trip through strtok looks for the strings separated by the NULLs.


Why don't they tell you about that in c++ books? Is it a well kept secret or something? I didn't know it could be done until someone on here (experts exchange) told me about it.
>>Why don't they tell you about that in c++ books? Is it a well kept secret or something?

If you look at the function decleration you can see that the second parameter declared as: const char *strDelimit and not char delimit (ie it is array of chars and not one char). more over, in MSDN it says that the second parameter is: "Set of delimiter characters" - so much for the conspiracy theory :-)


Author of the Year 2009

I'm pretty sure I was a comma on the grassy knoll.  In fact, several people saw a semicolon there.  Ya, that's the ticket.... we all saw a bunch of colons and spaces talking together about using tabs illicitly...

Explore More ContentExplore courses, solutions, and other research materials related to this topic.