#include <string.h>
#include <stdio.h>
int main(void)
{
char input[22] = "abc,d,eee,fff,ggg,hhh";
char *p;
/* strtok places a NULL terminator
in front of the token, if found */
p = strtok(input, ",");
/* A second call to strtok using a NULL
as the first parameter returns a pointer
to the character following the token */
while (p)
{
printf("%s\n", p); //d
p = strtok(NULL, ",");
}
return 0;
}
程序运行结果:
abc
d
eee
fff
ggg
hhh
调用方式 : char *strtok(char *str1, char *str2);
说明 : strtok()函数的原型在string.h中
功能说明:函数strtok()返回字符串str1中指向一个由str2所指定的字符或者字符串的分隔符的指 针,当没有要返回的分隔符时,就返回一个空指针。
函数strtok()实际上修改了有str1指向的字符串。每次找到一个分隔符后,一个空(NULL)就被放到分隔符处,函数用这种方法来连续查找该字符串。