_str指被分解的字符串 delim指分隔符字符串 返回类型是指针
strtok()用来将字符串分割成一个个片段。参数s指向欲分割的字符串,参数delim则为分割字符串中包含的所有字符。当strtok()在参数s的字符串中发现参数delim中包含的分割字符时,则会将该字符改为\0 字符。在第一次调用时,strtok()必需给予参数s字符串,往后的调用则将参数s设置成NULL。每次调用成功则返回指向被分割出片段的指针。
example:
"fafdsas,dkslafkaf,dsaf,dsa,as,a,kfldadg" ","
第一次调用 "fafdsas'\0'(第二次从这里指向开始)dkslafkaf,dsaf,dsa,as,a,kfldadg"
"fafdsas'\0'dkslafkaf'\0'dsaf,dsa,as,a,kfldadg"
"fafdsas'\0'dkslafkaf'\0'dsaf'\0'dsa,as,a,kfldadg"
"fafdsas'\0'dkslafkaf'\0'dsaf'\0'dsa'\0'as,a,kfldadg"
"fafdsas'\0'dkslafkaf'\0'dsaf'\0'dsa'\0'as'\0'a,kfldadg"
"fafdsas'\0'dkslafkaf'\0'dsaf'\0'dsa'\0'as'\0'a'\0'kfldadg"
最后指向最后'\0' 返回NULL
#include <string.h>
#include <stdio.h>
int main()
{
char arr[128] = { '\0'};
char* str[128];
int i = 0;
int z = 0;
gets(arr);
str[z] = strtok(arr," ");
while( str[z] != NULL )
{
z++;
str[z] = strtok(NULL," ");第二次使用时可以不用写arr,而是写NULL
}
for( i = 0 ; i < z; i++)
{
printf("%s\n",str[i]);
}
return 0;
}