When using check50 or inputting the provided texts, my program passes all but one test; the "complex single sentence" test returns a Grade 9 instead of Grade 8 (Coleman-Liau index returns 8.785455 and is rounded up). I've checked my code against some of the other examples people have posted on here to see if I can spot anything. I'm using some of the functions included in libraries on the CS50 website (which might be the issue) and counted out the words/letters/sentences from the text against the ones returned by my loop, and cant find any issue. My maths looks right to me, and I've checked it against 'correct' code from here. My counters are ints but cast as floats during equations.

int main(void)
    // Get user input
    string text = get_string("Text: ");

    int letters = 0;
    int sentences = 0;
    int i = 0;
    int words = 1;

    // Count words, letters and sentences
        int al = isalpha(text[i]);
        if (al > 0)
            //printf("letters: %i\n", letters);
        // Checks for space
        int sp = isspace(text[i]);
        if(sp > 0)
            // Checks that space is followed by letter
            al = isalpha(text[i + 1]);
            if (al > 0)
               //printf("words: %i\n", words);
        // checks for punctuation
        if (text[i] == '.' || text[i] == '!' || text[i] == '?')
            //printf("sentences: %i\n", sentences);
    while (i < strlen(text));

    //printf("letters: %i\nwords: %i\nsentences: %i\n", letters, words, sentences);

    // Calc L
    float L = ((float) letters / (float) words * 100.0);
    //printf("L: %f\n", L);

    // Calc S
    float S = ((float) sentences / (float) words * 100.0);
    //printf("S: %f\n", S);

    // Calc index
    float index = (0.0588 * L - 0.296 * S - 15.8);
    //printf("index: %f\n", index);

    // Print results
    if (index >= 16)
        printf("Grade 16+\n");
    else if (index < 1)
        printf("Before Grade 1\n");
        printf ("Grade %.f\n", round(index));

Here are the check50 results;

Results for cs50/problems/2020/x/readability generated by check50 v3.0.10
:) readability.c exists
:) readability.c compiles
:) handles single sentence with multiple words
:) handles punctuation within a single sentence
:( handles more complex single sentence
    expected "Grade 8\n", not "Grade 9\n"
:) handles multiple sentences
:) handles multiple more complex sentences
:) handles longer passages
:) handles questions in passage
:) handles reading level before Grade 1
:) handles reading level at Grade 16+

I originally coded this problem, had this bug, tried to debug and ended up rewriting. That usually works for me, but I've still got the same bug. It did cross my mind that it's an issue with the test, but probably not.

If anyone can help me spot the likely glaring mistake I've overlooked, I'd be very appreciative.


What is happening there, is that you did your word count asuming that the next caracter would be alphabetical, here: al = isalpha(text[i + 1]);, but in this case, the next caracter are quotation marks so it's not counting a couple of words.

  • You're a great human being. – JoeH May 31 '20 at 7:36
  • Tks. It helped me al well =) – Rafael Reis Araújo Sep 6 '20 at 15:02

You must log in to answer this question.

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