I'm answering Question 4 and I think I have a good grasp of how Main reads each word.

My question is specifically about "...!=EOF" that is mentioned many times.

I thought EOF is End of File? In the speller code, it seems EOF signifies end of each word in the text file.

So the end of each word in text files are represented by EOF? Sorry if this is obvious but I don't remembering hearing or reading about it yet.


I've been working through this pset as well. Here's how I understand what main is doing with respect to !=EOF:

The if condition

if (isalpha(c) || (c == '\'' && index > 0))

tests if a character is either a letter or an apostrophe (as long as the apostrophe doesn't come first as index[0]).

The else if condition that follows

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

        // update counter

terminates the current word by adding the null character because it's finding the new line character \n there. (Recall how if you had to malloc space for a char*, you would have to account for the sentinel value at the end.) If it's not a letter and it's not an apostrophe, c must indicate the end of a word, and the program responds as such.

Even though one word has ended, the loop continues since it will go through this process of individually looking at each character until it finally reaches EOF.

  • "The else if condition... terminates the current word by adding the null character because that's what it's finding in the text itself" is not quite right. The null character is only added to mark the end of the string stored in memory. In the file, there is no end of string marker. The end of each word is detected by reading the \n new line character that delineates each word from the next. If you're reading in each line at a time, you get a whole word at a time. The read stops with the \n. If you're reading in a character at a time, the new line marker still marks the end of each word. – Cliff B Feb 2 '16 at 7:32
  • Thanks for the clarification. I'll fix what I posted. – Peter Feb 2 '16 at 13:36

The for loop reads in each character using fgetc, only taking in letters and apostrophes. EOF is the value returned by fgetc (defined to be -1) when there are no more characters to be read in the text file, which is why you're testing against the condition !=EOF.

You must log in to answer this question.

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