0

CODE:

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

int main(void)
{

// DECLARE LETTER, WORD & SENTENCE COUNT

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

// DECLARE THE USERINPUT AND ITS COUNTER

string userinput;

int checkinput;

//OBTAIN A VALID INPUT

do
{

userinput = get_string("input text: ");

checkinput = strlen(userinput);

}

while (checkinput <= 0);

// CREATE BOOLEAN EXPRESSION FOR IDENTIFYING LOWERCASES, UPPERCASES, WORDS AND SENTENCES

int lettersIterate = 0;

// ITERATE THROUGH THE TEXT TO FIND LETTER COUNT

for (; lettersIterate <= checkinput; lettersIterate++)
{

    if (isalpha(userinput[lettersIterate]))
    {

        letters++;

    }

    if (userinput[lettersIterate] == ' ')
    {

        words ++;

    }

    if (userinput[lettersIterate] == '.' || userinput[lettersIterate] == '?' || 
userinput[lettersIterate] == '!')
    {

    sentences++;

    }

 }

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

// DECLARE AND ASSIGN THE VARIABLES FOR THE FORMULA

float L = (letters / words)*100;
float S = (sentences / words)*100;

// EXECUTE THE FORMULA

float INDEX = 0.0588 * L - 0.296 * S - 15.8;

int grade = roundf(INDEX);

if (grade > 16)
{

    printf("Grade 16+\n");

}

if(grade < 1)
{

    printf("Below Grade 1\n");

}

else
{

    printf("Grade %i\n", grade);

}
}

EXAMPLE TESTING RESULT:

./readability
input text: hey man. what have i done wrong here?
letters: 28
words: 8
sentences: 2
Grade 2

EXAMPLE EXTRACT RESULT (DESIRED GRADE: "Grade 5")

~/Week_2/pset2/ $ ./readability

input text: Harry Potter was a highly unusual boy in many ways. For one thing, he hated the summer 
holidays more than any other time of year. For another, he really wanted to do his homework, but was 
forced to do it in secret, in the dead of the night. And he also happened to be a wizard.

letters: 214
words: 56
sentences: 4

Grade 2

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 "letters: 96\nw..."
:( handles punctuation within a single sentence
expected "Grade 9\n", not "letters: 72\nw..."
:) handles more complex single sentence
:( handles multiple sentences
expected "Grade 5\n", not "letters: 214\n..."
:( handles multiple more complex sentences
expected "Grade 10\n", not "letters: 250\n..."
:) handles longer passages
:) handles questions in passage
:( handles reading level before Grade 1
expected "Before Grade 1...", not "letters: 29\nw..."
:( handles reading level at Grade 16+
expected "Grade 16+\n", not "letters: 184\n..."
0
float L = (letters / words)*100;
float S = (sentences / words)*100;

In this code, letters, words, and sentences are all ints, so when you divide them, the result rounds down. For the Harry Potter text, L ends up as 300, and S ends up as 0. For those to correctly come out as floats, you'll have to cast one or more of the integer values to floats when you divide. That would look like (float) letters.

| improve this answer | |

You must log in to answer this question.

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