-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreadability.c
113 lines (99 loc) · 2.89 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
107
108
109
110
111
112
113
// Includes
#include <stdio.h>
#include <math.h>
#include <string.h>
#include <ctype.h>
#include <stdlib.h>
// Delcare function
int get_index(char *s);
char *get_string(const char *prompt);
int main(void)
{
// Prompt user for text input
char *text = get_string("Text: ");
// Declare function
int index = get_index(text);
// Print output, which is the grade level of the provided text
// If index number is 16 or higher
if (index >= 16)
{
printf("Grade 16+\n");
}
// If the index number is less than 1
else if (index < 1)
{
printf("Before Grade 1\n");
}
// Final case
else
{
printf("Grade %d\n", index);
}
}
int get_index(char *s)
{
// Declare variable to store the number of letters.
int letters = 0;
// Declare variable to store the number of sentences.
int sentences = 0;
// Declare variable to store the number of words.
int words = 0;
for (int i = 0; i < strlen(s); i++)
{
// Increment letter count
char ch = s[i];
if (isalpha(ch))
{
letters++;
}
// Increment word count
if (isspace(ch))
{
words++;
}
// Increment Sentences
if (ch == '.' || ch == '!' || ch == '?')
{
sentences++;
}
}
// Account for last word since the word increment for loop misses the last word
words++;
// Declare and define variable for average number of letters per 100 words in the text
float L = (letters * 100.0f) / words;
// Declare and define variable for average number of sentences per 100 words in the text
float S = (sentences * 100.0f) / words;
// Declare and define variable calculated from the Coleman-Liau index
return round(0.0588 * L - 0.296 * S - 15.8);
}
// get_string function. Source: https://stackoverflow.com/questions/48282630/troubles-creating-a-get-string-function-in-c
char *get_string(const char *prompt)
{
char temp[350] = ""; // Maximum number of characters
int size,count;
while ( 1)
{
printf ( "%s",prompt);
if ( fgets ( temp, sizeof temp, stdin)) {
if ( !strchr ( temp, '\n')) {//no newline
printf("\x1B[31mError\x1B[0m: too long.\n%s",prompt);
size = strlen ( temp);
do {
fgets ( temp, sizeof temp, stdin);//read more and discard
size += strlen ( temp);
} while (!strchr ( temp, '\n'));//loop until newline found
printf("size :%i\n",size);
continue;//re prompt
}
break;
}
else {
fprintf ( stderr, "fgets problem\n");
return NULL;
}
}
temp[strcspn ( temp,"\n")] = '\0';//remove newline
char *word = malloc(strlen ( temp) + 1);
strcpy ( word, temp);
return word;
}