Advertisement
noob339

Untitled

Nov 16th, 2021
47
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.54 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <cs50.h>
  3. #include <math.h>
  4. #include <string.h>
  5. #include <ctype.h>
  6.  
  7. //This program will determine the grade level of your text based on an index the coleman lieu index
  8.  
  9.  
  10. //our prototypes
  11. int countLetters(string letters);
  12. int countWords(string words);
  13. int countSentences(string sentence);
  14.  
  15. int main(void)
  16. {
  17.     //we get the string
  18.     string x = get_string("Enter text: ");
  19.    
  20.     //we set up the calculation for the coleman index
  21.     float L = ((float) countLetters(x) / countWords(x) * 100);
  22.     float S = ((float) countSentences(x) / countWords(x) * 100);
  23.     float index = (0.0588 * L) - (0.296 * S) - 15.8;
  24.    
  25.    
  26.     //we set 3 paths, one for below 1st grade and one for grade 16 and above
  27.     //everything in between is just printed out
  28.     if(index < 1.00 )
  29.     {
  30.         printf("Before Grade 1 ");
  31.     }
  32.     else if(index >= 16.00)
  33.     {
  34.         printf("Grade 16+")
  35.     }
  36.     else
  37.     {
  38.         printf("Grade: %f\n", round(index));
  39.     }
  40.    
  41. }
  42. //This function checks for letters using the isalpha function from the ctype library as cs50 hinted lol
  43. //The function does all the heavy lifting, if the statement is true it adds it to the running total
  44. int countLetters(string letters)
  45. {
  46.    
  47.     int n = strlen(letters);
  48.     int letterSum = 0;
  49.    
  50.     for (int i = 0; i < n; i++)
  51.     {
  52.         if (isalpha(letters[i]))
  53.         {
  54.             letterSum++;
  55.         }
  56.     }
  57.    
  58.     //printf("%i letters \n", letterSum);
  59.    
  60.     return letterSum;
  61. }
  62.  
  63. //This function counds the amount of words in the string, and assumes it ends with either a letter of punctuation mark
  64. //We use the spaces to determine the amount of words and add 1 more at the end to our running total
  65. //Checks the elements off the array to search for spaces
  66. int countWords(string words)
  67. {
  68.     int n = strlen(words), j = 0;
  69.     char space = ' ';
  70.    
  71.    
  72.     for (int i = 0; i < n; i++)
  73.     {
  74.         if(space == words[i])
  75.         {
  76.             j++;
  77.         }
  78.     }
  79.    
  80.     j++;
  81.    
  82.     //printf("%i words \n", j);
  83.    
  84.     return j;
  85. }
  86.  
  87. //this function loops through the elements of the array of the string and checks for 3 punctuaton marks
  88. //for each punctuation we can assume for these purposes the sentence has ended
  89.  
  90. int countSentences(string sentence)
  91. {
  92.     int n = strlen(sentence), f = 0;
  93.    
  94.    
  95.    
  96.     for (int i = 0; i < n; i++)
  97.     {
  98.         if(sentence[i] == '.' || sentence[i] == '?' || sentence[i] == '!')
  99.         {
  100.             f++;
  101.         }
  102.     }
  103.    
  104.     return f;
  105. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement