Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <cs50.h>
- #include <ctype.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)
- {
- // Enter the text of reading
- string text = get_string("Text: ");
- int letters = count_letters(text);
- int words = count_words(text);
- int sentences = count_sentences(text);
- // Coleman-Liau Index
- int L = (float)letters / (float)words * 100;
- int S = (float)sentences / (float)words * 100;
- int grade = round(0.0588 * L - 0.296 * S - 15.8);
- if (grade >= 16)
- {
- printf("Grade 16+\n");
- }
- else if (grade < 1)
- {
- printf("Before Grade 1\n");
- }
- else
- {
- printf("Grade %i\n", grade);
- }
- }
- // Find number of letters/words/sentences in text
- int count_letters(string text)
- {
- int i = 0;
- int letters = 0;
- for (i = 0; i < strlen(text); i++)
- if (isalnum(text[i]))
- {
- letters++;
- }
- return letters;
- }
- int count_words(string text)
- {
- int words = 1;
- int i = 0;
- for(i = 0; i < strlen(text); i++)
- if (isalnum(text[i]) && isblank(text[i - 1]))
- {
- words++;
- }
- return words;
- }
- int count_sentences(string text)
- {
- int sentences = 0;
- int i = 0;
- for(i = 0; i < strlen(text); i++)
- if (i > 0 && (text[i] == '.' || text[i] == '!' || text[i] == '?') && isalnum(text[i - 1]))
- {
- sentences++;
- }
- return sentences;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement