我一直在尝试使用空格作为分隔符来标记字符串,但它不起作用。有人对为什么它不起作用有建议吗?
编辑:标记化使用:
strtok(string, " ");
代码如下
pch = strtok (str," ");
while (pch != NULL)
{
printf ("%s\n",pch);
pch = strtok (NULL, " ");
}
最佳答案
这样做:
char s[256];
strcpy(s, "one two three");
char* token = strtok(s, " ");
while (token) {
printf("token: %s\n", token);
token = strtok(NULL, " ");
}
注意:strtok
修改字符串的标记,因此它不能是 const char*
。
关于c - C 中的字符串标记,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28741953/