So i have been working on an C project and im stuck on this problem, basically Im making a program where the user can enter their name and I later print it out to a file, the first and last name of the user is split with a comma however, optional whitespace in around the comma is allowed. This is what i have been using the read the user names:
char lastName[20];
char firstName[20];
char line[LINESIZE];
while(fgets(line,LINESIZE,stdin)) {
if((sscanf(line," %s , %s ",lastName,firstName)) == 2) {
/*process name */
}
}
However, the only time it reads input successfully is when the user inputs:
john , doe
which matches the %s , %s I have, how can i make it such that something like:
john, doe
john ,doe
Can all work?
I have also tried
sscanf(line,"%s%[],%[]%s");
This doesnt cause compilation error, but it doesnt process input meaning it doesnt match the %s , %s
This will isolate the names so that any permutation of
"Doe , John"
filters out the whitespace