I don’t see your problem with strtok
, it would be perfect in this case I think.
In pseudo-code:
line = getline();
split_line_into_tokens(line);
if tokens[0] == "command1" {
if tokens_num > 2 {
error("to many arguments to command1");
} else if tokens_num < 2 {
error("command1 needs one argument");
} else {
do_command_1(tokens[1]);
}
} else {
error("unknown command");
}
In the above pseudo-code, split_line_into_tokens()
uses strtok
to create an array of tokens, using space as the separator. If strtok
returns an empty string (not NULL
) then there is more than one space used and you skip that. The split_line_into_tokens
creates the tokens
array, which first entry contains the command, and the remaining contains the arguments. The variable tokens_num
is set to the number of tokens in the array.
solved how to separate a number from a string [closed]