I’m writing a C program where the user can input a string of 1-3 digits followed by a backslash and then another 1-3 digits or they can enter 1-3 digits, followed by a comma, then another 1-3 digits and there is no limit to how many times they can iterate this.
I need to determine whether the input delimiter is a backslash or comma (to determine what to do with the numbers) and put the numbers into an array.
The way I was thinking of doing this was to using strtok as follows. The string is inputted as char *token.
op_tok1 = strtok(token, "\\");
if(op_tok1 != NULL)
{
/* Process numbers */
return;
}
op_tok2 = strtok(token, ",");
if(op_tok2 != NULL)
{
/* Process other numbers */
return;
}
This works for anything delimetered with a backslash, but not with a comma. I believe this is because strtok messes with the token variable. Is this true? Is there a better way to go about this? thanks!
There are certainly ways I’d consider better. If you can depend reasonably well on the format of the input (i.e., really being three digits followed by one of the allowed delimiters), you could do something like: