函数原型:char *strtok(char *s,const char *delim)
函数功能:按照给定的分隔符(delim)将字符串分割开
描述:strtok()用来将字符串分割成一个个片段,参数s指向将要被分隔的字符串,参数delim则为分隔字符串,当strtok()在参数s的字符串中发现到参数delim的分隔字符是,则会将该字符改为 ‘\0’ 字符,在第一次调用时,strtok()必须给与参数s字符串,往后的调用则将参数s设置成NULL。每次调用成功则返回被分隔片段指针。
代码示例
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
int main()
{
char example[100] = "晚安,玛卡巴卡,来,抽根华子";
char *accept;
accept = strtok(example,",");
while(accept!=NULL)
{
cout << accept << endl;
accept = strtok(NULL,",");
}
return 0;
}
示例输出
晚安
玛卡巴卡
来
抽根华子