Advertisement
jamestha3d

cs50 readability

Sep 26th, 2022
956
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.75 KB | None | 0 0
  1. #include <cs50.h>
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include <math.h>
  5.  
  6. int count_letters(string text);
  7. int count_words(string text);
  8. int count_sentences(string text);
  9.  
  10. int main(void)
  11. {
  12.     string text = get_string("Text: ");
  13.     //printf("%i \n %i \n %i \n", count_letters(text), count_words(text), count_sentences(text));
  14.  
  15.     float num_letters = (float) count_letters(text);
  16.     float num_words = (float) count_words(text);
  17.     float num_sentences = (float) count_sentences(text);
  18.     //index = 0.0588 * L - 0.296 * S - 15.8
  19.     //L is the avg of letters per 100 words
  20.     float L = (num_letters / num_words) * 100.0;
  21.     //S is the avg of sentences per 100 words.
  22.     float S = (num_sentences / num_words) * 100.0;
  23.  
  24.     int index = (int) round(0.0588 * L - 0.296 * S - 15.8);
  25.     //printf("%f %f %f\n", num_letters, num_words, num_sentences);
  26.     //printf("%f %f %i\n", L, S, (int) round(I));
  27.     if (index > 1 && index < 16)
  28.     {
  29.         printf("Grade %i\n", index);
  30.     }
  31.     else if (index < 1)
  32.     {
  33.         printf("Before Grade 1\n");
  34.     }
  35.     else
  36.     {
  37.         printf("Grade 16+\n");
  38.     }
  39. }
  40.  
  41. int count_letters(string text)
  42. {
  43.     int count = 0;
  44.     for (int i = 0, length = strlen(text); i < length; i++)
  45.     {
  46.         int number = (int) text[i];
  47.         if ((number >= 97 && number <= 122) || (number >= 65 && number <= 90))
  48.         {
  49.             count ++;
  50.         }
  51.     }
  52.  
  53.     return count;
  54. }
  55. int count_words(string text)
  56. {
  57.     int count = 0;
  58.     for (int i = 0, length = strlen(text); i < length; i++)
  59.     {
  60.         //if at the last character end loop
  61.         if (i == length - 1)
  62.         {
  63.             //current char
  64.             int number = (int) text[i];
  65.             int prev_number = (int) text[i - 1];
  66.  
  67.             if ((number > 65 && number < 90) || (number > 97 && number < 122))
  68.             {
  69.                 count ++;
  70.             }
  71.             else if (prev_number != 32)
  72.             {
  73.                 count ++;
  74.             }
  75.  
  76.         }
  77.         else
  78.         {
  79.             //current char
  80.             int number = (int) text[i];
  81.             //next char
  82.             int next_number = (int) text[i + 1];
  83.  
  84.             //if the next char is a space
  85.             if (next_number == 32)
  86.             {
  87.                 //check if current char is not a space
  88.                 if (number != 32)
  89.                 {
  90.                     count ++;
  91.                 }
  92.  
  93.             }
  94.         }
  95.  
  96.     }
  97.     return count;
  98. }
  99.  
  100. int count_sentences(string text)
  101. {
  102.     int count = 0;
  103.     for (int i = 0, length = strlen(text); i < length; i++)
  104.     {
  105.         int number = (int) text[i];
  106.         if (number == 33 || number == 63 || number == 46)
  107.         {
  108.             count++;
  109.         }
  110.     }
  111.     return count;
  112. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement