LINK TO ASSIGNMENT: https://cs50.harvard.edu/x/2024/psets/2/readability/ heres my code for the CS50x Readability assignment
for an input of "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." an output of "Grade 5" is needed...
but i keep geting "Grade 4", since it rounds down but i cant manipulate it without breeaking another case output needed...
an help would be appreciated
#include <cs50.h>
#include <ctype.h>
#include <math.h>
#include <stdio.h>
// A number of “readability tests” have been developed over the years that define formulas for
// computing the reading level of a text. One such readability test is the Coleman-Liau index. The
// Coleman-Liau index of a text is designed to output that (U.S.) grade level that is needed to
// understand some text. The formula is
// index = 0.0588 * L - 0.296 * S - 15.8
// where L is the average number of letters per 100 words in the text, and S is the average number
// of sentences per 100 words in the text.
int main(void)
{
// get text input
string text = get_string("Text: ");
char currentcharacter = 'A';
int number_of_periods = 0;
int number_of_spaces = 0;
int number_of_letters = 0;
int number_of_words = 0;
int number_of_sentences = 0;
int number_of_characters = 0;
double gradelevel = 0;
char letters[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
// cycle thru all text characters
for (int i = 0; text[i] != '\0'; i++)
{
// loop thru all characters in letters
currentcharacter = toupper(text[i]);
// check all letters for a match to either letter period(ot exclamations) or spaces
for (int j = 0; j < 25; j++)
{
if (currentcharacter == letters[j])
{
number_of_letters++;
break;
}
else if (currentcharacter == ' ')
{
number_of_spaces++;
break;
}
else if (currentcharacter == '.' || currentcharacter == '!' || currentcharacter == '?')
{
number_of_periods++;
break;
}
}
// count number of characters in text
number_of_characters++;
}
// check number of periods (number of periods = sentences)
number_of_sentences = number_of_periods;
// check number of spaces (number of spaces + 1 = number of words)
number_of_words = number_of_spaces + 1;
double L = ((double) number_of_letters * 100) / (double) number_of_words;
double S = ((double) number_of_sentences * 100) / (double) number_of_words;
gradelevel = (0.0588 * L) - (0.296 * S) - 15.8;
// debug prints
// printf("number_of_spaces: %d\n", number_of_spaces);
// printf("number_of_periods: %d\n", number_of_periods);
// printf("number_of_characters: %d\n", number_of_characters);
printf("number_of_letters: %d\n", number_of_letters);
printf("number_of_words: %d\n", number_of_words);
printf("number_of_sentences: %d\n", number_of_sentences);
printf("L: %f\n", L);
printf("S: %f\n", S);
printf("gradelevel : %f\n", gradelevel);
if (gradelevel < 1)
{
printf("Before Grade 1\n");
}
else if (gradelevel >= 16)
{
printf("Grade 16+\n");
}
else
{
printf("Grade %.f\n", round(gradelevel));
}
// index = 0.0588 * L - 0.296 * S - 15.8 ..... means reading level =
// where L is the average number of letters per 100 words in the text, and S is the average
// number of sentences per 100 words in the text.
return 0;
}