I am trying to understand the 'spell-check each word in text' part of speller.c in pset5.

How do we ever get an index >0? Index is set to 0 in the beginning and then the first if condition checks whether the character is alphabetic OR an apostrophe AND index >0. Only if this condition is met there is a index++. So even if it is alphabetic, this condition can't be met for the first letter of the word, doesn't it? What am I overlooking?

Thanks a lot in advance for your help! kafe


It's checking two separate conditions. Look at the test:

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

The first checks to see if the character is an alpha. If it is, then process it. The second test, (c == '\'' && index > 0) checks to see if the character is an apostrophe AND that the apostrophe is not the first character (i.e., index > 0).

Also, you need to look at the parentheses that dictate what is processed together. This is essentially if ( a || (b) ) where a is the isalpha() test and (b) is actually two conditions that must both be met - it must be an apostrophe AND it must not be the first character. More precisely, it would be if( A || ( B && C ) meaining that either A is true, or BOTH B & C are true. If A is true, B and C are ignored because the whole statement is automatically true. If A is false, then both B and C are checked. If A is false and B is then found to be false, then the whole statement is false and C is not checked. If A is false and B is true, then C is finally checked. Only then, if A is false, and B and C are both true, the whole statement is true.

As a practical matter, if the first char is an alpha, the statement is true, then the loop comes back around and index is incremented and is greater than 0.

If this answers your question, please click on the check to accept. Let's keep up on forum housekeeping. ;-)

  • Awesome! Thanks a lot for this very comprehensive answer.
    – kafe
    Oct 20 '15 at 23:43

You must log in to answer this question.

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