-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreadability.c
More file actions
81 lines (76 loc) · 1.6 KB
/
readability.c
File metadata and controls
81 lines (76 loc) · 1.6 KB
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
#include <cs50.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <math.h>
int letter_count(string sentence);
int word_count(string sentence);
int sentence_count(string sentence);
int main(void)
{
string sentence = get_string("Text: ");
int L = letter_count(sentence);
int W = word_count(sentence);
int S = sentence_count(sentence);
double L_avg = (L / (double) W) * 100;
double S_avg = (S / (double) W) * 100;
double grade1 = 0.0588 * L_avg - 0.296 * S_avg - 15.8;
int grade = round(grade1);
if (grade >= 16)
{
printf("Grade 16+\n");
}
else if (grade < 1)
{
printf("Before Grade 1\n");
}
else
{
printf("Grade %i\n", grade);
}
}
int letter_count(string sentence)
{
int count = 0;
for (int i = 0; i < strlen(sentence); i++)
{
sentence[i] = tolower(sentence[i]);
if (sentence[i] >= 'a' && sentence[i] <= 'z')
{
count++;
}
}
return count;
}
int word_count(string sentence)
{
int count = 1;
for (int i = 0; i < strlen(sentence); i++)
{
if (sentence[i] == ' ')
{
count++;
}
if (sentence[0] == ' ')
{
count--;
}
if (sentence[strlen(sentence) - 1] == ' ')
{
count--;
}
}
return count;
}
int sentence_count(string sentence)
{
int count = 0;
for (int i = 0; i < strlen(sentence); i++)
{
if (sentence[i] == '!' || sentence[i] == '?' || sentence[i] == '.')
{
count++;
}
}
return count;
}