-
Notifications
You must be signed in to change notification settings - Fork 11
/
Readability.c
52 lines (51 loc) · 1.41 KB
/
Readability.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
//Program based on Coleman-Liau index
//Any sequence of characters that ends with a . or a ! or a ? is a sentence according to that program
//So Mr. and Mrs. are 2 sentences which lower it's accuracy
#include <cs50.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <math.h>
int main(void)
{
string text = get_string("text: ");
float sentences = 0, letters = 0, words = 0;
int i = 0;
while (text[i] != '\0')
{
//this decrease the accuracy of the app but overall it's acceptable
if (text[i] == '.' || text[i] == '!' || text[i] == '?')
{
sentences++;
}
if (isalpha(text[i]))
{
letters++;
//i think there is a better way to decided wether or not this is a word but i didn't get it
if (isspace(text[i + 1]) || ispunct(text[i + 1]) || text[i + 1] == '\0')
{
if (text[i + 1] != '\'' && text[i + 1] != '-')
{
words++;
}
}
}
i++;
}
//using Coleman-Liau index formula
sentences *= 100 / words;
letters *= 100 / words;
float index = 0.0588 * letters - 0.296 * sentences - 15.8;
if (index >= 16)
{
printf("Grade 16+\n");
}
else if (index <= 1)
{
printf("Before Grade 1\n");
}
else
{
printf("Grade %i\n", (int)round(index));
}
}