-
Notifications
You must be signed in to change notification settings - Fork 0
/
readability.c
114 lines (101 loc) · 2.95 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
102
103
104
105
106
107
108
109
110
111
112
113
114
// Calculate approximate grade level of text using Coleman-Lau index
#include <cs50.h>
#include <ctype.h>
#include <math.h>
#include <stdio.h>
#include <string.h>
int count_letters(string letters);
int count_words(string words);
int count_sentences(string sentences);
int main(void)
{
// Prompt user for some text
string text = get_string("Text: ");
// Count the number of letters, words, and sentences in the text
int lcount = count_letters(text);
int wcount = count_words(text);
int scount = count_sentences(text);
// Calculate average letters per 100 words and average sentences per 100 words
float avletters = ((float) lcount / (float) wcount) * 100;
float avsentences = ((float) scount / (float) wcount) * 100;
// Compute the Coleman-Liau Index = 0.0588 * L - 0.296 * S - 15.8
float real_grade = (0.0588 * avletters) - (0.296 * avsentences) - 15.8;
// Round grade to nearest integer
int grade = round(real_grade);
// Print the grade level
if (grade >= 16)
{
printf("Grade 16+");
}
else if (grade < 1)
{
printf("Before Grade 1");
}
else
{
printf("Grade %i", grade);
}
printf("\n");
}
int count_letters(string letters)
{
// Initialize count
int lcount = 0;
// Loop through each of the text characters
for (int i = 0, len = strlen(letters); i < len; i++)
{
// If character is alphanumerical, add 1 to letter count
if (isalpha(letters[i]))
{
lcount++;
}
// If character is equal to ASCII value for apostrophe, add 1 to letter count
if (letters[i] == 96)
{
lcount++;
}
}
return lcount;
}
int count_words(string words)
{
// Initialize count
int wcount = 0;
// Loop through each of the text characters
for (int i = 0, len = strlen(words); i < len; i++)
{
// If character is a space add 1 to word count
if (isspace(words[i]))
{
wcount++;
}
}
// Return number of spaces + 1, equivalent to number of words
return wcount + 1;
}
int count_sentences(string sentences)
{
// Initialize count
int scount = 0;
// Loop through each of the text characters
for (int i = 0, len = strlen(sentences); i < len; i++)
{
// If character is equal to ASCII value of "!" add 1 to sentence count
if (sentences[i] == 33)
{
scount++;
}
// If character is equal to ASCII value of "." add 1 to sentence count
else if (sentences[i] == 46)
{
scount++;
}
// If character is equal to ASCII value of "?" add 1 to sentence count
else if (sentences[i] == 63)
{
scount++;
}
}
// return number of punctuation characters (eqv to sentences)
return scount;
}