-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProblem Set 2: Readability
42 lines (35 loc) · 1009 Bytes
/
Problem Set 2: Readability
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
#include <stdio.h>
#include <string.h>
#include <ctype.h>
int main() {
char text[1000];
printf("Write something: ");
fgets(text, sizeof(text), stdin);
text[strcspn(text, "\n")] = '\0';
int lettersCount = 0;
int wordCount = 0;
int senCount = 0;
for (int i = 0; text[i] != '\0'; i++) {
if (isalpha(text[i])) {
lettersCount++;
}
if (isspace(text[i])) {
wordCount++;
}
if (text[i] == '.' || text[i] == '?' || text[i] == '!') {
senCount++;
}
}
wordCount++; // Add one for the last word
float avgLetter = (float) lettersCount / wordCount * 100;
float avgSentence = (float) senCount / wordCount * 100;
float index = (0.0588 * avgLetter) - (0.296 * avgSentence) - 15.8;
if (index < 1) {
printf("Before Grade 1\n");
} else if (index > 16) {
printf("Grade 16+\n");
} else {
printf("Grade %d\n", (int) (index + 0.5));
}
return 0;
}