Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Command libraries
- #include <cs50.h>
- #include <stdio.h>
- #include <string.h>
- #include <ctype.h>
- #include <math.h>
- // Main program
- int main(void)
- {
- // Prompting user for text
- string text_prompt = get_string("Enter text for evaluation: ");
- // Count of TOTAL characters in string (anything)
- int total_length = strlen(text_prompt);
- // Setting up and implementing the counters
- int letter_count = 0;
- int word_count = 1;
- int sentence_count = 0;
- for(int n = 0; n < total_length; n++)
- {
- if (isalpha(text_prompt[n]))
- {
- letter_count++;
- }
- if (isblank(text_prompt[n]))
- {
- word_count++;
- }
- if ((text_prompt[n] == '!') || (text_prompt[n] == '?') || (text_prompt[n] == '.'))
- {
- sentence_count++;
- }
- }
- // Setting up grade level calculation
- float avg_letters = (100 * letter_count) / word_count;
- float avg_sentences = (100 * sentence_count) / word_count;
- int index = round(((0.0588 * avg_letters) - (0.296 * avg_sentences) - 15.8));
- // Printing result to user
- if (index > 16)
- {
- printf("Grade 16+\n");
- }
- else if (index < 1)
- {
- printf("Before Grade 1\n");
- }
- else
- {
- printf("Grade %d\n", index);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment