我只能建议你做错了,虽然有点难以准确地说出什么(通常在询问细节时你应该发布你的代码).示例程序,如下所示,似乎工作正常:
#include <stdio.h>
#include <string.h>
int main (void) {
char *s;
char str[] =
"This is a string,"
" with both spaces and commas,"
" for testing.";
printf ("[%s]\n", str);
s = strtok (str, ",");
while (s != NULL) {
printf (" [%s]\n", s);
s = strtok (NULL, ",");
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
它输出:
[This is a string, with both spaces and commas, for testing.]
[This is a string]
[ with both spaces and commas]
[ for testing.]
Run Code Online (Sandbox Code Playgroud)
立即想到的唯一可能性是,如果你使用" ,"而不是",".在这种情况下,你会得到:
[This is a string, with both spaces and commas, for testing.]
[This]
[is]
[a]
[string]
[with]
[both]
[spaces]
[and]
[commas]
[for]
[testing.]
Run Code Online (Sandbox Code Playgroud)