Search code examples
csyntaxwhile-loopfgetc

why is while written here with no body?


why is the while loop written in this code with no body? what does it do? The index and length are both integers and I am reading from a txt file.

  // ignore alphabetical strings too long to be words
        if (index > LENGTH)
        {
            // consume remainder of alphabetical string
            while ((c = fgetc(fp)) != EOF && isalpha(c));

            // prepare for new word
            index = 0;
        }

Solution

  • Once it has parsed string of length LENGTH, it should ignore all the subsequent characters. Thus, in the body of the while loop:

    c = fgetc(fp)) != EOF //get c from file pointer and check if it is not EOF
    isalpha(c) //and also it is an alphabet because alphabets can only make a word
    

    If both conditions are true, keep parsing and ignoring. Moment you encounter a character other than alphabet or just an EOF, reset index and ma be go to next line.