文章链接:https://codemouse.online/archives/2020-05-25214656
- 参数原型
char *strtok(char s[], const char *delim)
- 说明:
strtok()用来将字符串分割成一个个片段。
参数s指向欲分割的字符串,参数delim则为分割字符串中包含的所有字符。
当strtok()在参数s的字符串中发现参数delim中包含的分割字符时,则会将该字符改为\0 字符。
在第一次调用时,strtok()必需给予参数s字符串,往后的调用则将参数s设置成NULL。
每次调用成功则返回指向被分割出片段的指针。
strtok函数会破坏被分解字符串的完整,调用前和调用后的s已经不一样了。
- demo
#include<string.h>
#include<stdio.h>
int main(void)
{
char input[16]="abc,d";
char*p;
/*strtok places a NULL terminator
infront of the token,if found*/
p=strtok(input,",");
if(p)
printf("%s\n",p);
/*Asecond 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);
return 0;
}
本文介绍了如何使用C语言中的strtok函数来分割字符串。通过示例代码详细展示了strtok函数的基本用法,包括参数设置、字符串分割过程及返回值解释。
1017

被折叠的 条评论
为什么被折叠?



