-
Notifications
You must be signed in to change notification settings - Fork 45
/
readability.c
101 lines (79 loc) · 2 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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
/**
* CS50x 2021
* Problem Set 2: readability.c
*/
#include <stdio.h>
#include <cs50.h>
#include <stdbool.h>
#include <math.h>
int main(void)
{
int coleman_Liau_index_formula(string text);
void print_grade(int grade);
int grade;
string text;
text = get_string("Text: ");
grade = coleman_Liau_index_formula(text);
print_grade(grade);
}
int coleman_Liau_index_formula(string text)
{
int formula(int letters, int words, int sentences);
bool is_alpha_num(char c);
bool is_space(char c);
bool is_end(char c);
int i = 0, letters = 0, words = 0, sentences = 0;
bool is_word;
while (text[i]) {
char character = text[i];
if (is_alpha_num(character)) {
letters++;
is_word = true;
} else if (character == '-' && is_word) {
is_word = true;
} else if ((is_space(character) || (character == ',')) && is_word) {
words++;
is_word = false;
} else if (is_end(character) && is_word) {
words++;
is_word = false;
sentences++;
}
i++;
}
return formula(letters, words, sentences);
}
void print_grade(int grade)
{
if (grade < 1)
printf("Before Grade 1\n");
else if (grade >= 16)
printf("Grade 16+\n");
else
printf("Grade %i\n", grade);
}
bool is_alpha_num(char c)
{
return ((c >= 'a') && (c <= 'z')) ? true :
((c >= 'A') && (c <= 'Z')) ? true :
((c >= '0') && (c <= '9')) ? true : false;
}
bool is_space(char c)
{
return c == ' ' ? true : false;
}
bool is_end(char c)
{
return c == '.' ? true :
c == '!' ? true :
c == '?' ? true : false;
}
int formula(int letters, int words, int sentences)
{
float L, S;
// Average number of letters per 100 words.
L = (float) letters / words * 100;
// Average number of sentences per 100 words .
S = (float) sentences / words * 100;
return (int) round(0.0588 * L - 0.296 * S - 15.8);
}