Advertisement
Guest User

Untitled

a guest
Jun 28th, 2024
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.58 KB | None | 0 0
  1. #include <cs50.h>
  2. #include <stdio.h>
  3. #include <math.h>
  4. #include <string.h>
  5. #include <ctype.h>
  6.  
  7. //declaring functions
  8. int count_letters(string text);
  9. int count_words(string text);
  10. int count_sentences(string text);
  11.  
  12. int main(void)
  13. {
  14.  
  15. string text = get_string("text: ");
  16.  
  17.  
  18. int letters = count_letters(text);
  19. int words = count_words(text);
  20. int sentences = count_sentences(text);
  21.  
  22. float L;
  23. float S;
  24. float index;
  25.  
  26. L = (float) (100 * letters) / words;
  27. S = (float) (100 * sentences) / words;
  28.  
  29. index = (0.0588 * L) - (0.296 * S) - 15.8;
  30.  
  31. printf("index: %f\n", index);
  32.  
  33.  
  34. }
  35.  
  36. //function to count letters
  37.  
  38. int count_letters(string text)
  39. {
  40. int letters = 0;
  41. int len = strlen(text);
  42.  
  43. for(int i = 0; i < len; i++)
  44. {
  45. letters = letters + 1;
  46. }
  47.  
  48.  
  49. for(int j = 0; j < len; j++)
  50. {
  51. if (isspace(text[j]))
  52. {
  53. letters = letters - 1;
  54. }
  55. }
  56. return letters;
  57. }
  58.  
  59. //function to count words
  60.  
  61. int count_words(string text)
  62. {
  63. int spaces = 0;
  64.  
  65. int words;
  66.  
  67. int len = strlen(text);
  68.  
  69. for(int j = 0; j < len; j++)
  70. {
  71. if (isspace(text[j]))
  72. {
  73. spaces = spaces + 1;
  74. }
  75. }
  76.  
  77. words = spaces + 1;
  78. return words;
  79. }
  80.  
  81. //function to count sentences
  82. int count_sentences(string text)
  83. {
  84. int len = strlen(text);
  85.  
  86. int sentences = 0;
  87.  
  88. for(int j = 0; j < len; j++)
  89. {
  90. if (ispunct(text[j]))
  91. {
  92. sentences = sentences + 1;
  93. }
  94. }
  95. return sentences;
  96. }
  97.  
  98.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement