请给我解释一下strtok()
函数的工作原理.手册上说它把字符串分解成记号.我无法从手册中理解它的实际功能.
我在str
和*pch
上添加了手表,以判断其工作情况.当第一个while循环发生时,str
的内容仅为"this".下面显示的输出是如何打印在屏幕上的?
/* 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;
}
Output:
Splitting string "- This, a sample string." into tokens: This a sample string