forked from Mayconpm/CS50x_2021
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreadability.c
84 lines (74 loc) · 1.75 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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#include <stdio.h>
#include <cs50.h>
#include <string.h>
#include <ctype.h>
#include <math.h>
int count_letters(string text);
int count_words(string text);
int count_sentences(string text);
int readability_index(int letters, int words, int sentences);
int main(void)
{
string text = get_string("Text: ");
int letters = count_letters(text);
int words = count_words(text);
int sentences = count_sentences(text);
int index = readability_index(letters, words, sentences);
if (index < 1)
{
printf("Before Grade 1\n");
}
else if (index >= 16)
{
printf("Grade 16+\n");
}
else
{
printf("Grade %i\n", index);
}
}
int count_letters(string text)
{
int letters = 0;
for (int i = 0, n = strlen(text); i < n; i++)
if (isalpha(text[i]))
{
letters ++;
}
return letters;
}
int count_words(string text)
{
int words = 0;
for (int i = 0, n = strlen(text); i < n; i++)
{
if (text[i] == '-' && isalpha(text[i - 1])) {}
else if (text[i] == 39 && isalpha(text[i - 1])) {}
else if (!isalpha(text[i]) && isalpha(text[i - 1]))
{
words ++;
}
}
return words;
}
int count_sentences(string text)
{
int sentences = 0;
for (int i = 0, n = strlen(text); i < n; i++)
{
int character = text[i];
if (character == 46 || character == 33 || character == 63)
{
sentences ++;
}
}
return sentences;
}
int readability_index(int letters, int words, int sentences)
{
float words_per_100 = words / 100.0;
float L = letters / words_per_100;
float S = sentences / words_per_100;
int index = round(0.0588 * L - 0.296 * S - 15.8);
return index;
}