what does that line do in speller.c
?
while((c = fget(fb)) != EOF && isalpha(c));
CS50 Stack Exchange is a question and answer site for students of Harvard University's CS50. It only takes a minute to sign up.
Sign up to join this communitybasically the following:
fp
.c
.EOF
(i.e., the end-of-file indicator).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.
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
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.