strtok使用了错误的分隔符

sve*_*nus 0 c delimiter strtok

strtok当我将分隔符指定为","时,为什么我在空格后分解我的字符串?

pax*_*blo 7

我只能建议你做错了,虽然有点难以准确地说出什么(通常在询问细节时你应该发布你的代码).示例程序,如下所示,似乎工作正常:

#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)