0

include

include

include

include

include

int main(void)

{ // Ask user for input string s = get_string("Text:");

// variable to store letter, word, and sentence count
int letterCount = 0;
int wordCount = 0;
int sentCount = 0;

if (isalnum(s[0])) { wordCount = 1; }

// looks for character in string
for (int i = 0, n = strlen(s); i < n; i++)
{
    //checks if character in the string is in the alphabet
    if (isalnum(s[i]))
    {
        // adds to number of letters in the string
        letterCount++;
    }
        // counts words in the sentence
    if (i < n - 1 && isspace(s[i]) && isalnum(s[i + 1]))
    {
        wordCount++;
    }

    // counts the number of sentences
    if (i > 0 && ((s[i]) == '!' || (s[i]) == '?' || (s[i]) == '.') && isalnum(s[i - 1]))
    {
        sentCount++;
    }
}

//Colman-Liu formula to calculate reading grade level
float grade = (0.0588 * (letterCount * 100.0 / wordCount)) - (0.296 * (sentCount * 100.0 / wordCount)) - 15.8;

if (grade <= 1)
{
    printf("Before Grade 1\n");
}
else if (grade < 16)
{
    printf("Grade %.f\n", roundf(grade));
}
else
{
    printf("Grade 16+\n");
}

}

When i enter the following it shows up Grade 9, instead of the expected Grade 8, all the other quotes work as expected:

Alice was beginning to get very tired of sitting by her sister on the bank, and of having nothing to do: once or twice she had peeped into the book her sister was reading, but it had no pictures or conversations in it, "and what is the use of a book," thought Alice "without pictures or conversation?"

I'm not sure if there is anything wrong with my code or not because I get the expected output for all the other prompts. The check50 also shows everything works properly besides for this one.

1
  1. isalnum has no place here. isalpha checks for letters. Shouldn't matter as we have no digits.
  2. The word count is overcomplicated. Per problem specification, anything separated by spaces should be considered a word, and there are only single spaces, and no leading/trailing ones. So simply count the space characters (plus one), no further checks.
  3. Same for sentence count. Per specification, just count the .!?.
  4. The grade should be rounded to the closest integer before you check whether it is < 1 and so on.

You must log in to answer this question.

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