I am getting the following string as input from the user: "id,name,age,grade,country,city"
-id consists of digits 0-9 only -name consists of a-zA-z letters -grade consists of digits 0-9 only -country consists of a-zA-z letters, white spaces and '-' dashes. -city consists of a-zA-z letters, white spaces and '-' dashes.
I am trying to speharete each field into its own string.
my code is:
char id[MAX_STRING_SIZE], name[MAX_STRING_SIZE], grade[MAX_STRING_SIZE], age[MAX_STRING_SIZE],
country[MAX_STRING_SIZE],city[MAX_STRING_SIZE];
sscanf(userInput,SEPARATOR,id,name,grade,age,country,city);
where userInput is a string in the form "id,name,age,grade,country,city"
and const char SEPARATOR[] = "%[^,],%[^,],%[^,],%[^,],%[^,\n],%s";
however, this code breaks with certain inputs, such as "id,,age,grade,country,city"
sscanfreturn value and issue an error message if the input is invlaid.strchr), replace them with '\0', and set the pointer. Or even usestrtok. Usingstrtokis usually a mistake, but it's better than the using sscanf for this.