-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreadability.c
61 lines (52 loc) · 1.3 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
#include <stdio.h>
#include <cs50.h>
#include <string.h>
#include <ctype.h>
#include <math.h>
int count_letters(string text);
int main(void)
{
// Get input text from the user
string text = get_string("Text: ");
// Initialise counts for letters, words and sentences
float letters = 0, words = 1, sentences = 0;
// Calculate string length
int length = strlen(text);
// Loop through each element in string
for (int i = 0; i < length; i++)
{
// Check if character is alphabetic
if (isalpha(text[i]))
{
// Add one to letter count
letters++;
}
// Check if character is a space
if (isspace(text[i]))
{
// Add one to word count
words++;
}
// Check if the end of a sentence
if (text[i] == '.' || text[i] == '!' || text[i] == '?')
{
// Add one to sentence count
sentences++;
}
}
// Calculate index
int index = round(0.0588 * (letters / words) * 100 - 0.296 * (sentences / words) * 100 - 15.8);
// Print Grade level to user
if (index < 1)
{
printf("Before Grade 1\n");
}
else if (index < 16)
{
printf("Grade %i\n", index);
}
else
{
printf("Grade 16+\n");
}
}