0

I believe I've written the readability.c program correctly to count letters, words, and sentences based on readability prompt and calculate the grade index. But whenever I test the grade index, I'm getting certain grade levels incorrect.

For example:

Text input: In my younger and more vulnerable years my father gave me some advice that I've been turning over in my mind ever since.

The return is Grade 9 but it should be Grade 8.

Can anyone provide me with assistance on how to fix this?

Thank you! :)

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

int count_letters(string text);
int count_words(string text);
int count_sentences(string text);


int main(void)
{
    //Get string of text from human
    string text = get_string("Text: ");

    //Implement function of count_letters
    int countletters = count_letters(text);

    //Implement function of count_words
    int countwords = count_words(text);

    //Implement function of count_sentences
    int countsentences = count_sentences(text);

    //Grade index formula (index = 0.0588 * L - 0.296 * S - 15.8)
        //L is average number of letters in 100 words (countletters/countwords * 100)
        //S is average number of sentences in 100 words (countsentences/countwords * 100)
    float l = (((float) countletters / (float) countwords) * 100.0);
    float s = (((float) countsentences / (float) countwords) * 100.0);
    float index = ((0.0588 * l) - (0.296 * s) - 15.8);

    float grade = floor(index);

    //If grade index is lower than 1
    if (index < 1)
    {
        printf("Before Grade 1\n");
    }
    //If grade index is higher than 16+
    else if (index > 16)
    {
        printf("Grade 16+\n");
    }
    //If grade index is above 1 and below 16 print the grade index that's calculated
    else
    {
        printf("Grade %.f\n", grade);
    }
}


//count_letters function performance
int count_letters(string text)
{
    //Declaring lettercount variable
    int lettercount = 0;

    //Looping over the letters in the text input by human
    for (int i = 0, n = strlen(text); i < n; i++)
    {
        //If the character is a letter
        if (isalpha(text[i]))
            //Incrememnt lettercount
            lettercount++;
    }
    return lettercount;
}

//count_words function performance - count number of words in a string
int count_words(string text)
{
    //Declaring wordcount variable
    int wordcount = 0;

    //Looping over the number of spaces to determine number of words in the text input by human
    for (int i = 0, n = strlen(text); i < n; i++)
    {
        if (i < n - 1 && isspace(text[i]))
        {
            wordcount++;
        }
    }
    return wordcount;
}


int count_sentences(string text)
{
    //Declaring sentencecount variable
    int sentencecount = 0;

    //Looping over the number of punctuation marks to determine number of stences input by human
    for (int i = 0, n = strlen(text); i < n; i++)
    {
        if (text[i] == '.' || text[i] == '!' || text[i] == '?')
        {
            sentencecount++;
        }
    }
    return sentencecount;
}


  
[1]: https://i.stack.imgur.com/y8e21.png

1 Answer 1

0

There are two issues with the code.

First,the formula for calculating the grade level isn't quite right.

float index = ((0.0588 * l) - (0.296 * s) - 15.8);

The result of all that calculating needs to be rounded. This problem is giving one-off errors (the result is off by one) for some inputs. I suspect that you were trying to accomplish this with the floor function, but floor doesn't round up. Adding the round function will render the floor statement unnecessary.

Second, the word counts are always going to be off by one as well. I'll let you figure out why, but here's a hint. How does the last word get counted? What characters follow the last word in the input data?

If this answers your question, please click on the check mark to accept. Let's keep up on forum maintenance. ;-)

1
  • This was very helpful thank you! I realised it was my word count and when i went into it i realised it was because the last word doesn't have a space following so it never counts! >.< Anyways, managed to fix that by declaring the variable word count to start at 1 instead of 0 and change the from floor to round and voila! Thank you so much!
    – karlares
    Sep 14, 2022 at 14:27

You must log in to answer this question.

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