Advertisement
Guest User

Untitled

a guest
Nov 26th, 2023
240
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 <ctype.h>
  3. #include <stdio.h>
  4. #include <string.h>
  5. #include <math.h>
  6.  
  7. int count_letters(string text);
  8. int count_words(string text);
  9. int count_sentences(string text);
  10. int main(void)
  11.  
  12. {
  13.     // Enter the text of reading
  14.     string text = get_string("Text: ");
  15.     int letters = count_letters(text);
  16.     int words = count_words(text);
  17.     int sentences = count_sentences(text);
  18.  
  19.     // Coleman-Liau Index
  20.     int L = (float)letters / (float)words * 100;
  21.     int S = (float)sentences / (float)words * 100;
  22.     int grade = round(0.0588 * L - 0.296 * S - 15.8);
  23.     if (grade >= 16)
  24.     {
  25.         printf("Grade 16+\n");
  26.     }
  27.     else if (grade < 1)
  28.     {
  29.         printf("Before Grade 1\n");
  30.     }
  31.     else
  32.     {
  33.         printf("Grade %i\n", grade);
  34.     }
  35. }
  36.  
  37. // Find number of letters/words/sentences in text
  38. int count_letters(string text)
  39. {
  40.     int i = 0;
  41.     int letters = 0;
  42.     for (i = 0; i < strlen(text); i++)
  43.  
  44.     if (isalnum(text[i]))
  45.     {
  46.         letters++;
  47.     }
  48.     return letters;
  49. }
  50.  
  51. int count_words(string text)
  52. {
  53.     int words = 1;
  54.     int i = 0;
  55.     for(i = 0; i < strlen(text); i++)
  56.     if (isalnum(text[i]) && isblank(text[i - 1]))
  57.     {
  58.         words++;
  59.     }
  60.     return words;
  61. }
  62.  
  63. int count_sentences(string text)
  64. {
  65.     int sentences = 0;
  66.     int i = 0;
  67.     for(i = 0; i < strlen(text); i++)
  68.     if (i > 0 && (text[i] == '.' || text[i] == '!' || text[i] == '?') && isalnum(text[i - 1]))
  69.     {
  70.         sentences++;
  71.     }
  72.     return sentences;
  73. }
  74.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement