Advertisement
Guest User

Readability

a guest
Apr 22nd, 2024
155
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.47 KB | None | 0 0
  1. #include <cs50.h>
  2. #include <ctype.h>
  3. #include <math.h>
  4. #include <stdio.h>
  5. #include <string.h>
  6.  
  7. int count_letters(string text);
  8. int count_words(string text);
  9. int count_sentences(string text);
  10.  
  11. int main (void)
  12. {
  13.     string user_input = get_string("Text:");
  14.     int letters = count_letters(user_input);
  15.     int words = count_words(user_input);
  16.     int sentences = count_sentences(user_input);
  17.  
  18.     double grade = 0.0588 * (((float)letters / words) * 100) - 0.296 * (((float)sentences / words) * 100) - 15.8;
  19.  
  20.     if(grade >= 16)
  21.     {
  22.         printf("Grade 16+");
  23.     }
  24.     else if(grade < 1)
  25.     {
  26.         printf("Before Grade 1");
  27.     }else
  28.     {
  29.         printf("Grade: %i\n", (int)round(grade));
  30.     }
  31.  
  32. }
  33.  
  34. int count_letters(string text)
  35. {
  36.     int letters = 0;
  37.  
  38.     for (int i = 0, length = strlen(text); i < length; i++)
  39.     {
  40.         if(isalpha(text[i]) != 0)
  41.         {
  42.             letters++;
  43.         }
  44.     }
  45.     return letters;
  46. }
  47.  
  48. int count_words(string text)
  49. {
  50.     int words = 0;
  51.  
  52.     for (int i = 0, length = strlen(text); i < length; i++)
  53.     {
  54.         if(isspace(text[i]) != 0)
  55.         {
  56.             words++;
  57.         }
  58.     }
  59.     return words += 1;
  60. }
  61.  
  62. int count_sentences(string text)
  63. {
  64.     int sentences = 0;
  65.  
  66.     for (int i = 0, length = strlen(text); i < length; i++)
  67.     {
  68.         if(text[i] == ',' || text[i] == '.' || text[i] == '!'|| text[i] == '?')
  69.         {
  70.             sentences++;
  71.         }
  72.     }
  73.     return sentences;
  74. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement