In the main for loop, how does the program know when the end of one word has been reached?

for loop looks like this:

for (int c = fgetc(fp); c != EOF; c = fgetc(fp))

if I understand it correctly, fegtc() will read till the end of stream (i.e. our file) pointed to by fp. How does the program know when the full word has been read?

Further, in the for loop, we don't check if the current char returned by fgetc is a null string (indicating end of string), rather we just check the value of index:

else if (index > 0)
        // terminate current word
        word[index] = '\0';

        // update counter

        // prepare for new word
        index = 0;

But index can be > 0, even if we haven't reached the end of the string.


Inside the for loop you mention, there are three condition:

// allow only alphabetical characters and apostrophes
if (isalpha(c) || (c == '\'' && index > 0))
   // (...) character belongs to the current word

// ignore words with numbers (like MS Word can)
else if (isdigit(c))
    // (...) if the current word has a number in it, just consume
    // the remaining alphanumeric characters and don't count it a word

// we must have found a whole word
else if (index > 0)
    // (...) else we read something that's not an alphanumeric or an
    // apostrophe (something line a new line or a space), so end the word

The reason we check for a space, and not for a null byte \0, is because we don't read strings from the file, we read chars, and all those chars are not stored as strings, but as consecutive bytes, so they don't end with a \0.

Hope this answers your question! Happy Coding! :)

  • hey Chris, thanks for your post, I'm still confused though: 1. first if: we check if c is alphabetical or an apostrophe. If yes – store it in the word array. 2. else if: if c is a digit: ignore the rest of the word. this loop runs until fgetc hits EOF. But why EOF and not a space or a NULL character? Are the characters we are reading from fp stored in one long singe string? – Anton K Aug 24 '16 at 17:48
  • The loop inside the condition for digit is not consuming until EOF, but until a non alphanumeric is read, like a space or a new line. – ChrisG Aug 24 '16 at 22:18
  • aha...so, basically, this else if statement else if (index > 0) indicates that fgetc returned a space and now we can terminate the current word stored in word array. is that correct? – Anton K Aug 25 '16 at 7:27
  • Partially. It doesn't mean directly that a space has been read, but that something has been read that it's not an alphabetical character or apostrophe (first if), neither a number (second if). The index > 0 checks that something else than the above has been read, and that we have read at least one character for the current word. Then, as you correctly said, the word is terminated and start the loop again. – ChrisG Aug 25 '16 at 7:53
  • I finally get it. Thank you. I was confused by this: for (int c = fgetc(fp); c != EOF; c = fgetc(fp)). I thought that the loop runs until it hits end of file; furthermore, I didn't see any explicit check of whether fgetc has read a space (indicating the end of the current word). I now understand that else if (index > 0) does this job. Would you like to post this as an answer, so I can mark it as a solution? – Anton K Aug 25 '16 at 8:43

You must log in to answer this question.

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