Advertisement
Guest User

readability test

a guest
Aug 1st, 2022
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.52 KB | None | 0 0
  1. #include <cs50.h>
  2. #include <stdio.h>
  3. #include <ctype.h>
  4. #include <string.h>
  5. #include <math.h>
  6.  
  7. int count_letters(string t);
  8. int count_words(string t);
  9. int count_sentences(string t);
  10. int formula(int l, int w, int s);
  11.  
  12. int main(void)
  13. {
  14.     string text = get_string("Text: ");
  15.  
  16.     int l_ = count_letters(text);
  17.     int w_ = count_words(text);
  18.     int s_ = count_sentences(text);
  19.     int index = formula(l_, w_, s_);
  20.  
  21.     if (index >= 16)
  22.     {
  23.         printf("Grade 16+\n");
  24.     }
  25.     else if (index < 16 && index >= 1)
  26.     {
  27.         printf("Grade %i\n", index);
  28.     }
  29.     else
  30.     {
  31.         printf("Before Grade 1\n");
  32.     }
  33. }
  34.  
  35. int count_letters(string t)
  36. {
  37.     int letters = 0;
  38.     int len = strlen(t);
  39.     for(int x = 0; x < len; x++)
  40.     {
  41.         if (isalpha(t[x]) != 0)
  42.         {
  43.             letters++;
  44.         }
  45.     }
  46.     return letters;
  47. }
  48.  
  49. int count_words(string t)
  50. {
  51.     int spaces = 0;
  52.     int len = strlen(t);
  53.     for(int x = 0; x < len; x++)
  54.     {
  55.         if (t[x] == 32)
  56.         {
  57.             spaces++;
  58.         }
  59.     }
  60.     return spaces + 1;
  61. }
  62.  
  63. int count_sentences(string t)
  64. {
  65.     int sentences = 0;
  66.     int len = strlen(t);
  67.     for(int x = 0; x < len; x++)
  68.     {
  69.         if(t[x] == 33 || t[x] == 46 || t[x] == 63)
  70.         {
  71.             sentences++;
  72.         }
  73.     }
  74.     return sentences;
  75. }
  76.  
  77. int formula(int l, int w, int s)
  78. {
  79.     float L = (l / w) * 100;
  80.     float S = (s / w) * 100;
  81.     float index = 0.0588 * L - 0.296 * S - 15.8;
  82.     return round(index);
  83. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement