int Split(char* src,const char* delimiters,char* output[]){
int output_index = 0;
char* p = strtok(src,delimiters);
while (p != NULL){
//切分结果放在数组中
output[output_index] = p;
output_index++;
p = strtok(NULL, delimiters);
}
return output_index;
}
int main(){
char str[] = "aaa,bbb,ccc,ddd";
char* output[100] = { 0 };
int n = Split(str, ",", output);
for (int i = 0; i < n; ++i){
printf("%s\n", output[i]);
}
system("pause");
return 0;
}
也可以这么用
int main(){
char str1[] = "sing,dance-rap+basketball";
puts(strtok(str1, ",-+"));
puts(strtok(NULL, ",-+"));
puts(strtok(NULL, ",-+"));
puts(strtok(NULL, ",-+"));
system("pause");
return 0;
}