0
// prepare to spell-check
int index = 0, misspellings = 0, words = 0;
char word[LENGTH+1];

// spell-check each word in text
for (int c = fgetc(fp); c != EOF; c = fgetc(fp))
{
    // allow only alphabetical characters and apostrophes
    if (isalpha(c) || (c == '\'' && index > 0))
    {
        // append character to word
        word[index] = c;
        index++;

        // 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;
        }
    }

    // ignore words with numbers (like MS Word can)
    else if (isdigit(c))
    {
        // consume remainder of alphanumeric string
        while ((c = fgetc(fp)) != EOF && isalnum(c));

        // prepare for new word
        index = 0;
    }

    // we must have found a whole word
    else if (index > 0)
    {
        // terminate current word
        word[index] = '\0';

        // update counter
        words++;

        // check word's spelling
        getrusage(RUSAGE_SELF, &before);
        bool misspelled = !check(word);
        getrusage(RUSAGE_SELF, &after);

        // update benchmark
        time_check += calculate(&before, &after);

        // print word if misspelled
        if (misspelled)
        {
            printf("%s\n", word);
            misspellings++;
        }

        // prepare for next word
        index = 0;
    }
}
2

The for loop in speller.c does not check the value of index in any of its conditionals. Rather, it checks whether fgetc() returns EOF in order to exit the loop after reading all the characters in the input file. The value of index is used in a different way within the body of the loop. Take a closer look and see if you can figure out what it is really used for.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .