0

I'm trying to break input in five parts using strtok(), with each part separated by colons

ab:c:X:cf:b

However, the user can also choose to not enter anything for some parts of the input

f::V:pq:

Is there a way to account for this using strtok? Here is my code so far but it is not working as intended.

        piece = strtok(action_def, ":");
        if (piece!=NULL) {
            execute
        }
        piece = strtok(NULL, ":");
        if (piece!=NULL) {
            execute
        }
        piece = strtok(NULL, ":");
        action_list[action_n]->name = *piece;
        piece = strtok(NULL, ":");
        if (piece!=NULL) {
            execute
        }
        // last char recorded by fgets is '\n'
        piece = strtok(NULL, "\n");
        if (piece!=NULL) {
            execute
        }
Gerhardh
  • 11,688
  • 4
  • 17
  • 39
pp182
  • 107
  • 8
  • 1
    Does this answer your question? [C: Parse empty tokens from a string with strtok](https://stackoverflow.com/questions/3375530/c-parse-empty-tokens-from-a-string-with-strtok) – 001 Oct 26 '20 at 13:13
  • Please do not add C to your title. There are tags used for that purpose. – Gerhardh Oct 26 '20 at 13:16
  • Just don't use strtok(). Use strspn()/strcspn() (or strchr()) Or just walk the sting in a loop. – wildplasser Oct 26 '20 at 18:40

0 Answers0