C - Determining which delimiter used - strtok()

前端 未结 3 1555
攒了一身酷
攒了一身酷 2021-02-14 08:05

Let\'s say I\'m using strtok() like this..

char *token = strtok(input, \";-/\");

Is there a way to figure out which token actually

3条回答
  •  暖寄归人
    2021-02-14 08:26

    man 3 strtok

    The strtok() and strtok_r() functions return a pointer to the beginning of each subsequent token in the string, after replacing the token itself with a NUL character. When no more tokens remain, a null pointer is returned.

    But with a little pointer arithmetic you can do something like:

    char* string = "Hello,World!";
    char* dup = strdup(string);
    
    char* world = strtok(string, ",");
    char delim_used = dup[world - string];
    
    free(dup);
    

提交回复
热议问题