commit 47327078735392fb091e3a051872231f733b0ad7 Author: bot50 Date: Sat Feb 24 18:57:42 2024 +0000 kukemuna-cs50/problems/2024/x/readability@20240224T185742.083682883Z diff --git a/readability.c b/readability.c new file mode 100644 index 0000000..e642bfc --- /dev/null +++ b/readability.c @@ -0,0 +1,80 @@ +#include +#include +#include +#include +#include + +int get_letters(string text); +int get_words(string text); +int get_sentences(string text); + +int main(void) +{ + // Prompt the user for some text + string text = get_string("Text:\n"); + + // Count the number of letters, words, and sentences in the text + int letters = get_letters(text); + int words = get_words(text) + 1; // +1 for last word + int sentences = get_sentences(text); + + // Compute the Coleman-Liau index + int index = round(0.0588 * (100 * letters / (float) words) - 0.296 * (100 * sentences / (float) words) - 15.8); + + // Print the grade level + if (index < 1) + { + printf("Before Grade 1\n"); + } + else if (index >= 16) + { + printf("Grade 16+\n"); + } + else + { + printf("Grade %i\n", index); + } + + // printf("# letters: %i\n", letters); + // printf("# words: %i\n", words); + // printf("# sentences: %i\n", sentences); +} + +int get_letters(string text) +{ + int letters = 0; + for (int i = 0, length = strlen(text); i < length; i++) + { + if (!isspace(text[i]) && !ispunct(text[i])) + { + letters++; + } + } + return letters; +} + +int get_words(string text) +{ + int words = 0; + for (int i = 0, length = strlen(text); i < length; i++) + { + if (isspace(text[i])) + { + words++; + } + } + return words; +} + +int get_sentences(string text) +{ + int sentences = 0; + for (int i = 0, length = strlen(text); i < length; i++) + { + if (text[i] == '!' || text[i] == '?' || text[i] == '.') + { + sentences++; + } + } + return sentences; +}