I am writing a program to validate a CSV file. The limitations on the format are:
- A line cannot end with a comma, it must end with a digit;
- A line cannot start with a comma, it must be a digit;
- A line can be blank;
- Only single digits are allowed as numbers (i.e. no numbers below 0 or greater than 9);
- Two commas cannot be next to each other, they must be separated by a digit.
EDIT:
I have made some changes to the code based on the feedback. Can someone please explain what the while loop is actually doing? And why isn’t the switch working? I am now getting 1’s for all input. Can I use isdigit in the switch?
#include <ctype.h>
#include <stdio.h>
int main()
{
int c;
while ((c = getchar()) != EOF )
{
switch(c)
{
case'0':
printf("1");
break;
case'1':
printf("1");
break;
case'2':
printf("1");
break;
case'3':
printf("1");
break;
case'4':
printf("1");
break;
case'5':
printf("1");
break;
case'6':
printf("1");
break;
case'7':
printf("1");
break;
case'8':
printf("1");
break;
case'9':
printf("1");
break;
case',' :
printf("1");
break;
case'\n' :
printf("1");
break;
default :
printf("0");
break;
}
}
}
1 Answer