Actually I need to handle situation like
I should be giving input as rows <n>
There should be space between ‘rows’ and number <n> or any single non numeric character.
I should be able to separate that string part and assign it to a char variable and number part to a int…
The string part should be then verified whether its a correct command or not.. If a wrong command is entered like “ada aad 99” or “adaha 9” or “adfad9”.. It should say “its wrong command”.
I tried to use strtok(), but it can’t handle strings where there isn’t NULL in between strings.. I tried to use $ sscanf(string,"%s %*c %d",str, &num);
but its even not working for all possibilities.
How can I do it?
I don’t see your problem with
strtok, it would be perfect in this case I think.In pseudo-code:
In the above pseudo-code,
split_line_into_tokens()usesstrtokto create an array of tokens, using space as the separator. Ifstrtokreturns an empty string (notNULL) then there is more than one space used and you skip that. Thesplit_line_into_tokenscreates thetokensarray, which first entry contains the command, and the remaining contains the arguments. The variabletokens_numis set to the number of tokens in the array.