C language: How to get the remaining string after using strtok() once

后端 未结 6 1791
名媛妹妹
名媛妹妹 2021-02-13 16:42

My string is \"A,B,C,D,E\"
And the separator is \",\"
How can I get the remaining string after doing strtok() once, that is \"B,C,D,E\"

char a[] = \"A,B,         


        
6条回答
  •  长发绾君心
    2021-02-13 17:15

    strtok remembers the last string it worked with and where it ended. To get the next string, call it again with NULL as first argument.

    char a[] = "A,B,C,D,E";
    const char *separator = ",";
    char *b = strtok(a, separator);
    while (b) {
        printf("element: %s\n", b);
        b = strtok(NULL, separator);
    }
    

    Note: This is not thread safe.

提交回复
热议问题