-
Notifications
You must be signed in to change notification settings - Fork 765
/
Copy pathReadability_cs50
121 lines (91 loc) · 2.77 KB
/
Readability_cs50
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
115
116
117
118
119
120
/*
Readability solution by Adarsh Singh
*/
#include <stdio.h>
#include <cs50.h>
#include <string.h>
#include <math.h>
int count_letters(string text); // function for counting letters
int count_words(string text); // function for counting words
int count_sent(string text); // function for counting sentences
int colemanformula(double L, double S); //Coleman-Liau index formula
int main(void)
{
string text = get_string("Text: "); // user input of text
float letters = count_letters(text); // storing the number of letters
//printf("%.1f letters(s)\n",letters);
float words = count_words(text); // storing the number of words
//printf("%.1f word(s)\n",words);
float sentences = count_sent(text); // storing the number of sentences
//printf("%.1f sentence(s)\n", sentences);
double L = (letters / words) * 100; // L is the average number of letters per 100 words in the text
double S = (sentences / words) * 100; // S is the average number of sentences per 100 words in the text.
int grade = colemanformula(L, S); // calling function of coleman formula
if (grade < 1) // checking grade condition
{
printf("Before Grade 1\n");
}
else if (grade > 1 && grade <= 16) // checking grade condition
{
printf("Grade %i\n", grade);
}
else
{
printf("Grade 16+\n");
}
}
int count_letters(string text) // function for counting the number of letters in user input
{
int count = 0;
for (int i = 0, n = strlen(text); i < n; i++)
{
if (text[i] >= 'A' && text[i] <= 'Z')
{
count = count + 1;
}
else if (text[i] >= 'a' && text[i] <= 'z')
{
count = count + 1;
}
else if (text[i] == ' ')
{
count = count + 0;
}
}
return count;
}
int count_words(string text) // function for counting the number of words in user input
{
int words = 0, n = strlen(text);
for (int i = 0; i <= n; i++)
{
if (text[i] == ' ')
{
words = words + 1;
}
}
if (text[n - 1] == '.' || text[n - 1] == '!' || text[n - 1] == '?')
{
words = words + 1;
}
return words;
}
int count_sent(string text) // function for counting the number of sentences in user input
{
int sent = 0, n = strlen(text);
for (int i = 0; i <= n; i++)
{
if (text[i] == '!' || text[i] == '.' || text[i] == '?')
{
sent = sent + 1;
}
}
return sent;
}
int colemanformula(double L, double S) // coleman forumla for finding out the grade
{
double index = 0;
index = (0.0588 * L) - (0.296 * S) - 15.8;
int grade = round(index);
return grade;
}