p = strtok(NULL, ","); 看不懂,请高手来帮忙一下!

newman0708 2003-10-13 07:50:15
#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, ",");
if (p) printf("%s\n", p); //abc

/* A second call to strtok using a NULL
as the first parameter returns a pointer
to the character following the token */
p = strtok(NULL, ",");
if (p) printf("%s\n", p); //d
  
//这里不知道它在做什么,看不懂,尤其是p = strtok(NULL, ","); 中的null,
请帮忙解释一下。


return 0;
}


谢谢!
...全文
5165 7 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
黑白猿 2003-10-13
  • 打赏
  • 举报
回复 2
调用方式 : char *strtok(char *str1, char *str2);
说明 : strtok()函数的原型在string.h中
功能说明:函数strtok()返回字符串str1中指向一个由str2所指定的字符或者字符串的分隔符的指 针,当没有要返回的分隔符时,就返回一个空指针。

函数strtok()实际上修改了有str1指向的字符串。每次找到一个分隔符后,一个空(NULL)就被放到分隔符处,函数用这种方法来连续查找该字符串。

例子:
#include <string.h>
#include <stdio.h>

int main()
{
char *p;
char str[100]="This is a test ,and you can use it";
p = strtok(str," "); // 注意,此时得到的 p为指向字符串:"This",即在第一个分隔 符前面的字符串,即每次找到一个分隔符后,一个空(NULL)就被放到分隔符处,所以此时NULL指针指向后面的字符串:"is a test ,and you can use it"。

printf("%s\n",p); // 此时显示:This

do
{
p = strtok(NULL, ","); // NULL 即为上面返回的指针,即字符串:
// "is a test ,and you can use it"。
if(p)
printf("|%s",p);
}while(p);

system("pause");
return 0;
}
daizh 2003-10-13
  • 打赏
  • 举报
回复
#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
redleaves 2003-10-13
  • 打赏
  • 举报
回复
strtok函数的功能是把一个字符串由指定的分隔符分成更小的字符串。
例如:"this is a demo"用" "(空格)来分隔,就可以得到"this","is","a","demo"四个单词。
strtok的用法:
第一步:初始化
strtok("this is a demo"," ");
第二步:得到其它的子字符串
strtok(NULL," ");
每执行一次第二步就可以得到一个子字符串,直到返回NULL,表示查找结束。
daizh 2003-10-13
  • 打赏
  • 举报
回复
char *strtok(char *s, char *delim);

功能:分解字符串为一组标记串。s为要分解的字符串,delim为分隔符字符串。

说明:首次调用时,s必须指向要分解的字符串,随后调用要把s设成NULL。strtok在s中查找包含在delim中的字符并用NULL('\0')来替换,直到找遍整个字符串。返回指向下一个标记串。当没有标记串时则返回空字符NULL。

newman0708 2003-10-13
  • 打赏
  • 举报
回复
能给我一个例子吗?

分割上面我的程序中"abc,d,eee,fff,ggg,hhh";

谢谢!
fwinfwin 2003-10-13
  • 打赏
  • 举报
回复
你说的那句是搜索下一个被','分隔开的字符!
以下来自msdn,希望对你有所帮助!

定义:
char *strtok(
char *strToken,
const char *strDelimit
);
作用
Find the next token in a string.

参数
【strToken 】
String containing token or tokens.
【strDelimit】
Set of delimiter characters.
返回值
Returns a pointer to the next token found in strToken. They return NULL when no more tokens are found. Each call modifies strToken by substituting a NULL character for each delimiter that is encountered
fengwl 2003-10-13
  • 打赏
  • 举报
回复
要把strtok的源程序拉出来看看就知道了。
大概是把字符串转变成k??

70,020

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧