2

what does that line do in speller.c?

while((c = fget(fb)) != EOF && isalpha(c));
2

basically the following:

  1. read a character from the stream pointed to by fp.
  2. store that character into the variable called c.
  3. ensure this character does not equal to EOF (i.e., the end-of-file indicator).
  4. ensure this character is an alphabetical character.
  5. proceed to the body of the loop if 3 & 4 are true.
| improve this answer | |
6

It's easier to explain in the context of the loop it is in:

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

So it runs if the string is too long to be a word. The idea is that fgetc gets each char left in the string, which moves the read pointer along in the file so that it will be ready to read in the next word.

That's what the comment consume remainder of alphabetical string is meant to explain. They don't need to keep c, but simply move the read pointer to the end of the word.

| improve this answer | |
  • note the AND isalpha(c) it will keep going as long as the it hasn't hit the end of the file AND the char is alphabetic. once it hits a space, that is not alphabetic so we know it's the end of that word. – curiouskiwi Sep 4 '15 at 8:14
  • you say that isalpha(c) stops when it hits a spacebut what if isalpha(c) hits a number ?? i think in this case the rest of the word will be considered a new word. – kawegan Sep 11 '15 at 9:32
  • it will never hit a digit in this section of code as it wouldn't pass that first if condition. – curiouskiwi Sep 12 '15 at 0:16
  • assume that a character passes the first if condition and the index is greater than (LENGTH). all we do is moving the cursor to reach the end of the word but the characters that the cursor passes are not checked by the first if condition so fget(c) int the while loop might hit a number and if that happens, the while loop stops . is that right ? – kawegan Sep 12 '15 at 21:02
  • 1
    Yes, if it hits a number at that point, index will be set to 0 and the for loop will start again. – curiouskiwi Sep 13 '15 at 8:24
2

Ok I get it, the key here is the && statement, how didn't I see this ugh, I feel stupid. Basically if it's a alphabetic char and not the end of the file, move the file read pointer to the next char. This means, if you hit a space or a weird char, then you stop consuming chars.

| improve this answer | |

You must log in to answer this question.

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