0
#include <stdio.h>
#include <cs50.h>
#include <string.h>
#include <ctype.h>
#include <math.h>

// Letters detects letters
// Words detects spaces
// Sentences detects periods, exclamation points, question marks

int main(void)
{
    int n, letters = 0, words = 1, sentences = 0;
    string text;
    
    text = get_string("Text: ");
    
    n = strlen(text);
    
    for (int i = 0; i < n; i++)
    {
        //letters
        
        if ((text[i] >= 'a' && text[i] <= 'z') || (text[i] >= 'A' && text[i] <= 'Z'))
        {
            letters++;
        }
        
        //words
        
        if (text[i] == ' ')
        {
            words++;
        }
        //sentences
        
        if (text[i] == '!' || text[i] == '.' || text[i] == '?')
        {
            sentences++;
        }
        
    }
    //Coleman-Liau formula
    
    float L = (100 * ((float) letters / (float) words));
    float S = (100 * ((float) sentences / (float) words));
        
    float grade0 = (0.0588 * L) - (0.296 * S) - 15.8;
    int grade1 = (round(grade0));
        
    if (grade1 >= 1 && grade1 < 16)
    {
        printf("Grade: %i", (int) grade1);
    }
    else if (grade1 < 1)
    {
        printf("Before Grade 1");
    }
    else 
    {
        printf("Grade 16+");
    }
    printf("\n");
}

on running check50:

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

confused as to what the issue is as expected "Grade 7\n", not "Grade: 7\n" is just "Grade 7\n" twice, similar to all the others

2 Answers 2

2

Did you notice that your code includes a colon after the word grade but check50 results don't? ;-)

The lesson here is that program specs are to be followed exactly and precisely. Close or similar doesn't get it done.

0
  1. when counting words, don't only check for spaces, because if you type "hello World" with 5 spaces in between 'hello' and 'world', words count will be 7 instead of 2. think about it as: if text[i] is 'space' AND next-char after current space is not 'space'.(otherwise aplhabet or punctuation....) the you've found a word.
  2. when checking for which grade level, start from low to high or vise versa.
  3. expected "Grade 7\n", not "Grade: 7\n"... remove : 'grade' and "7".

You must log in to answer this question.

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