-
Notifications
You must be signed in to change notification settings - Fork 2
/
Readability.c
88 lines (80 loc) · 1.84 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
#include <ctype.h>
#include <cs50.h>
#include <stdio.h>
#include <string.h>
#include <math.h>
int count_letters(string text);
int count_words(string text);
int count_sentences(string text);
int main(void)
{
//To get the text from user:
string text = get_string("Text: ");
int letters = count_letters(text);
int words = count_words(text);
int sentences = count_sentences(text);
//To print the no. of letters, words & sentences
printf("%d letters\n", letters);
printf("%d words\n", words);
printf("%d sentences\n", sentences);
//Calculation for Coleman-Liau index:
float L = ((float)letters/(float)words) * 100;
float S = ((float)sentences/(float)words) * 100;
float index;
index = 0.0588 * L - 0.296 * S - 15.8;
printf("index: %f\n", index);
//To round off the index to the nearest grade:
int grade = round(index*1);
//To print the grade:
if (grade < 1)
{
printf("Before Grade 1\n");
}
else if (grade >= 16)
{
printf("Grade 16+\n");
}
else
{
printf("Grade %d\n", grade);
}
}
//function to count letters:
int count_letters(string text)
{
int letters = 0;
for (int i = 0, n = strlen(text); i < n; i++)
{
if (isupper(text[i]) || islower(text[i]))
{
letters++;
}
}
return letters;
}
//function to count words:
int count_words(string text)
{
int words = 0;
for (int i = 0, n = strlen(text); i < n; i++)
{
if (text[i] == ' ')
{
words++;
}
}
return words + 1;
}
//function to count sentences:
int count_sentences(string text)
{
int sentence = 0;
for (int i = 0, n = strlen(text); i < n; i++)
{
if (text[i] == '.' || text[i] == '!' || text[i] == '?')
{
sentence++;
}
}
return sentence;
}