Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <cs50.h>
- #include <ctype.h>
- #include <math.h>
- #include <stdio.h>
- #include <string.h>
- int count_letters(string text);
- int count_words(string text);
- int count_sentences(string text);
- int main (void)
- {
- string user_input = get_string("Text:");
- int letters = count_letters(user_input);
- int words = count_words(user_input);
- int sentences = count_sentences(user_input);
- double grade = 0.0588 * (((float)letters / words) * 100) - 0.296 * (((float)sentences / words) * 100) - 15.8;
- if(grade >= 16)
- {
- printf("Grade 16+");
- }
- else if(grade < 1)
- {
- printf("Before Grade 1");
- }else
- {
- printf("Grade: %i\n", (int)round(grade));
- }
- }
- int count_letters(string text)
- {
- int letters = 0;
- for (int i = 0, length = strlen(text); i < length; i++)
- {
- if(isalpha(text[i]) != 0)
- {
- letters++;
- }
- }
- return letters;
- }
- int count_words(string text)
- {
- int words = 0;
- for (int i = 0, length = strlen(text); i < length; i++)
- {
- if(isspace(text[i]) != 0)
- {
- words++;
- }
- }
- return words += 1;
- }
- int count_sentences(string text)
- {
- int sentences = 0;
- for (int i = 0, length = strlen(text); i < length; i++)
- {
- if(text[i] == ',' || text[i] == '.' || text[i] == '!'|| text[i] == '?')
- {
- sentences++;
- }
- }
- return sentences;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement