-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathreadability.c
106 lines (85 loc) · 2.42 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
#include <stdio.h>
#include <cs50.h>
#include <ctype.h>
#include <math.h>
int count_letters(string input_text);
int count_words(string input_text);
int count_sentences(string input_text);
int get_index(int letters, int words, int sentences);
int main(void)
{
string input_text = get_string("Text: ");
// Call function to count number of letters in string
int letters = count_letters(input_text);
// Call function to count number of words in string
int words = count_words(input_text);
// Call function to count number of sentences in string
int sentences = count_sentences(input_text);
// Call function to get Coleman-Liau index of input text
int index = get_index(letters, words, sentences);
// If index is between 1-15
if (index >= 1 && index <= 15)
{
printf("Grade %i\n", index);
}
// If index is less than 1
else if (index <= 1)
{
printf("Before Grade 1\n");
}
// If index is greater than 15 (16+)
else
{
printf("Grade 16+\n");
}
}
int count_letters(string input_text)
{
int letters = 0;
// Loop through string until null character is reached
for (int i = 0; input_text[i] != '\0' ; i++)
{
// If current character is a alphabet
if (isalpha(input_text[i]) != 0)
{
letters++;
}
}
return letters;
}
int count_words(string input_text)
{
int words = 0;
// Loop through string until null character is reached
for (int i = 0; input_text[i] != '\0' ; i++)
{
// If current character is a space, it marks the end of a word
if (input_text[i] == ' ')
{
words++;
}
}
// Increse word count by one due to word at end of sentence that isn't followed by a space
return ++words;
}
int count_sentences(string input_text)
{
int sentences = 0;
// Loop through string until null character is reached
for (int i = 0; input_text[i] != '\0' ; i++)
{
// Assume if current character is '.', '!' or '?' it marks the end of a sentence
if (input_text[i] == '.' || input_text[i] == '!' || input_text[i] == '?')
{
sentences++;
}
}
return sentences;
}
int get_index(int letters, int words, int sentences)
{
float l = letters / (float)words * 100;
float s = sentences / (float)words * 100;
int index = round(0.0588 * l - 0.296 * s - 15.8);
return index;
}