-4

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

2 Answers2

0
while(1)

This is your problem.

Within this while loop, you have a switch statement full of break;s. These break;s will only get you out of the switch and will not get you out of the while loop. You have no way of getting out of this loop.

I have no earthly idea of what exactly the program is supposed to do (and I just spent 5 minutes fixing the formatting), so I can't currently make a recommended fix, but this is the problem.


Per @woolstar's comment, the inner while loop is unnecessary. The while(!feof(input)) can take care of repeatedly calling the switch statement. You will probably need to move the int state = 1; outside of this outer while loop however.

nhgrif
  • 61,578
  • 25
  • 134
  • 173
0

I think your approach is wrong. As I have understood from your explanation, the string must start with "XYZ1" and after this you may have no matter how many 1 (just 1, not other characters). So, it would be very simple to check for the first part using strncmp, and then check if remaining characters are all 1.

c=fgetc(input);
if(strncmp(c, "XYZ1", 4) == 0){
    //check if remaining characters are 1
}else{
    //the string does not match
}

Also, while(!feof(input)) is discouraged: Why is “while ( !feof (file) )” always wrong?

Community
  • 1
  • 1
Paul92
  • 8,827
  • 1
  • 23
  • 37