I want the problem to recognize words in that order: XYZ1111***
no matter how many '1' or '*' but it must have at least one '1' and XYZ must be in that exact order and always be included for the string to be valid. It must read from a file that I have written a lot of these words such as XYZ1
, XYZ1111*
, 1111*
and print ok
if the word meets the restrictions. When I run my program it just takes the name of the file and then does nothing. Here's my code:
#include<stdio.h>
#include<stdlib.h>
int main(int argc,char *argv[]) {
FILE *input;
char c;
if(argc >2) {
printf("Too much arguments");
exit(1);
} else if(argc==2) {
input=fopen(argv[1],"r");
if(input==NULL) {
printf("Unable to find file ");
exit(1);
}
} else {
input=stdin;
c=fgetc(input);
while (!feof(input)) {
if (c=='x') {
int state=1;
while(1) {
switch(state) {
case 1:
c=fgetc(input);
if (c=='Y')
state=2;
break;
case 2:
c=fgetc(input);
if(c=='Z')
state=3;
break;
case 3:
c=fgetc(input);
if (c==1)
state=4;
break;
case 4:
if (c=='1')
state=4;
else if(c=='*')
state=5;
else if(c=='\n' || c=='\t' || c==' ')
printf("ok");
break;
case 5:
if (c=='*')
state=5;
else if(c=='\n' || c=='\t' || c==' ')
printf("ok");
break;
} // end of switch
} // end of while(1)
} // end of if(c=='x')
} // end of while(!feof(input))
} // end of else
printf("bgika");
} // end of main