#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