-
Notifications
You must be signed in to change notification settings - Fork 0
/
readability.c
56 lines (48 loc) · 1.04 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
53
54
55
56
#include <cs50.h>
#include <ctype.h>
#include <math.h>
#include <stdio.h>
#include <string.h>
int main(void)
{
int letters = 0;
int words = 0;
int sentence = 0;
string input = get_string("Text: ");
if (strlen(input) > 0)
{
words = 1;
}
for (int i = 0, n = strlen(input); i < n; i++)
{
if (isalpha(input[i]))
{
letters += 1;
}
else if (input[i] == ' ' && i < n - 1 && input[i + 1] != ' ')
{
words += 1;
}
else if (input[i] == '.' || input[i] == '?' || input[i] == '!')
{
sentence += 1;
}
}
float L = (letters / (float) words) * 100;
float S = (sentence / (float) words) * 100;
float grade = 0.0588 * L - 0.296 * S - 15.8;
int grade_level = round(grade);
if (grade_level < 1)
{
printf("Before Grade 1\n");
}
else if (grade_level >= 16)
{
printf("Grade 16+\n");
}
else
{
printf("Grade %d\n", grade_level);
}
return 0;
}