forked from keshavsingh4522/hacktoberfest
-
Notifications
You must be signed in to change notification settings - Fork 0
/
readability.c
61 lines (48 loc) 路 1.23 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
57
58
59
60
61
// predicts grade-level of a text
// based on Coleman-Liau index
#include<cs50.h>
#include<math.h>
#include<string.h>
#include<stdio.h>
int main(void)
{
//set initial count for letters,words,sentences to 0
int letters = 0;
int words = 1;
int sentences = 0;
//promot user for text
string s = get_string("Text:\n");
//calculate number of letters,words and sentences
for (int i = 0, n = strlen(s); i < n; i++)
{
if ((s[i] >= 65 && s[i] <= 90) || (s[i] >= 97 && s[i] <= 122))
{
letters++;
}
if (s[i] == ' ')
{
words++;
}
if ((s[i] == '.' || s[i] == '?') || s[i] == '!')
{
sentences++;
}
}
// calculate "value" of index
float value = ((0.0588 * 100 * ((float) letters / (float) words)) - (0.296 * 100 * ((float) sentences / (float) words)) - 15.8);
// round-off index value to nearest integer
int index = round(value);
//printf grade of the text
if (index >= 1 && index < 16)
{
printf("Grade %i\n", index);
}
else if (index >= 16)
{
printf(" Grade 16+\n");
}
else if (index < 1)
{
printf("Before Grade 1\n");
}
}