From 26d7fa7fe13d18d0aa6d6c1d7081078a55a2af29 Mon Sep 17 00:00:00 2001 From: kukemuna Date: Sat, 24 Feb 2024 20:56:23 +0200 Subject: [PATCH] automated commit by check50 [check50=True] --- readability.c | 80 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 80 insertions(+) create mode 100644 readability.c 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; +}