Hi Guys,
I am stuck with an issue where not all the results are correct. I don’t know why does it not calculate Grade 7 and 8 correctly.
This is the output:
readability.c exists
readability.c compiles
handles single sentence with multiple words
** expected “Grade 7\n”, not “Grade 8\n”**
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+
text used to test is:
7: In my younger and more vulnerable years my father gave me some advice that I’ve been turning over in my mind ever since.
8: Alice was beginning to get very tired of sitting by her sister on the bank, and of having nothing to do: once or twice she had peeped into the book her sister was reading, but it had no pictures or conversations in it, “and what is the use of a book,” thought Alice “without pictures or conversation?”
#include <stdio.h>
#include <cs50.h>
#include <ctype.h>
#include <string.h>
#include <math.h>
int letters;
int words;
int sentences;
int main(void)
{
// User prompt for input
string text= get_string("Text: ");
// Get text length
int n = strlen(text);
// add +1 if the text starts with alphanumeric letters
if (isalnum(text[0]))
{
words = 1;
}
// count words
for (int i = 0; i < n; i++)
{
// count letters
if (isalnum(text[i]))
{
letters++;
}
// count words
if (i < n - 1 && isspace(text[i]) && isalnum(text[i + 1]))
{
words++;
}
// count sentences
if (i > 0 && (text[i] == '!' || text[i] == '?' || text[i] == '.') && isalnum(text[i - 1]))
{
sentences++;
}
}
// calculate Coleman-Liau index
int grade = round(0.0588 * (100 * letters / words) - 0.296 * (100 * sentences / words) - 15.8);
// debugger
//printf("letters: %i\n wordss: %i\n sentences: %i\n", letters, words, sentences);
// print result
if (grade <= 1)
{
printf("Before Grade 1\n");
}
else if (grade < 16)
{
printf("Grade %i\n", grade);
}
else
{
printf("Grade 16+\n");
}
}