- Jan 24, 2011
- 9,378
Please explain how the strtok() method works. According to the documentation, it splits the string into tokens. I can't figure out what it does based on the handbook.
When the first while loop began, the contents of str were merely "this." I added watches on str and *pch to test its functionality. How did the following output appear on the screen?
Code:
/* strtok example */
#include <stdio.h>
#include <string.h>
int main ()
{
char str[] ="- This, a sample string.";
char * pch;
printf ("Splitting string \"%s\" into tokens:\n",str);
pch = strtok (str," ,.-");
while (pch != NULL)
{
printf ("%s\n",pch);
pch = strtok (NULL, " ,.-");
}
return 0;
}