0

I could not found any explanation about the checking part of speller.c and this bothers me a bit not to understand it.

If we read the code below, this is what I understand:

  1. Check if c is an alphabetic or " ' " char (ASCII code, c is an int), then generate the word to be checked. And if the word is to big, just iterate through it until we reach a non-alphabetic char.
  2. Check if this is another ASCII char which is not a-z or " ' ".
  3. Check if index > 0 in order to close the current word and start a new one.

How do we reach the point 3? If number 1 checks if this is an alphabetic char and if the number 2 checks if this is something else, how do we reach the number 3 ?

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

    // ignore words with numbers (like MS Word can)
    else if (isdigit(c))
    {
        // consume remainder of alphanumeric string
        while ((c = fgetc(fp)) != EOF && isalnum(c));

        // prepare for new word
        index = 0;
    }

    // we must have found a whole word
    else if (index > 0)
    {
        // terminate current word
        word[index] = '\0';

        // update counter
        words++;

        // check word's spelling
        getrusage(RUSAGE_SELF, &before);
        bool misspelled = !check(word);
        getrusage(RUSAGE_SELF, &after);

        // update benchmark
        time_check += calculate(&before, &after);

        // print word if misspelled
        if (misspelled)
        {
            printf("%s\n", word);
            misspellings++;
        }

        // prepare for next word
        index = 0;
     }
    }
0

The alphabet characters, apostrophe and the 10 digits are only about half of the possible ASCII characters. What you are left with is the control codes and the special characters like & and #. Including the space character (ASCII 32). This code considers any non-alphabetic, non-digit character (except apostrophe) a word delimiter. They will get the the third branch. Here you will find an ASCII table.

2
  • thank you for the answer. I am still a bit confused. If Space or NUL (\0) are in the ASCII characters, which special characters can be used as a word delimiter ? – Ludovic Renevey Nov 15 '16 at 6:03
  • Space is commonly the word delimiter. isalpha(c) and isdigit(c) both evaluate to false when c is space, so the program takes the third branch. – DinoCoderSaurus Nov 15 '16 at 11:59

You must log in to answer this question.

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