温江做网站哪家好,进入公众号核酸检测,系统界面设计图,太原百度做网站多少钱目录 一#xff0c;strtok函数简介
二#xff0c;strtok函数的用法
三#xff0c;strtok函数的注意事项 一#xff0c;strtok函数简介 strtok函数可以帮助我们将一个字符串按照指定的分隔符进行分割#xff0c;从而得到我们想要的子字符串。 #x1f342;函数头文件strtok函数简介
二strtok函数的用法
三strtok函数的注意事项 一strtok函数简介 strtok函数可以帮助我们将一个字符串按照指定的分隔符进行分割从而得到我们想要的子字符串。 函数头文件
#include string.h 函数原型 char * strtok ( char * str, const char * delim ); str要分割的字符串 delim用于分割的字符集合 二strtok函数的用法
#include stdio.h
#include string.h
int main()
{char str[] applebanan.grape;//要分割的字符串char* p .;//用于分割的字符集合char* s NULL;for (s strtok(str, p); s ! NULL; s strtok(NULL, p)){printf(%s\n, s);}return 0;
}
运行结果 第一次调用strtok时传入要分割的字符串str和分隔符delim函数返回第一个子字符串的指针。后续再次调用strtok时只需要传入str为NULL函数会继续从上一次分割的位置开始返回下一个子字符串的指针。当没有更多的字符串可供返回时函数返回NULL。 三strtok函数的注意事项
在使用strtok函数时需要注意以下几点
1.strtok函数会修改原始字符串将分隔符替换为NULL字符因此在分割过程中需要注意原始字符串的备份。
#include stdio.h
#include string.h
int main()
{char str[] applebanan.grape;//要分割的字符串char buf[200] { 0 };strcpy(buf, str);//将数据拷贝一份处理str数组的内容char* p .;//用于分割的字符集合char* s NULL;for (s strtok(buf, p); s ! NULL; s strtok(NULL, p)){printf(%s\n, s);}return 0;
}
2.如果要分割的字符串中包含连续的分隔符strtok函数会忽略这些分隔符并返回空。