Advertisement
bocajbee

Readability

Jan 20th, 2020
309
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.05 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <math.h>
  4. #include <cs50.h>
  5. #include <ctype.h>
  6. #include <string.h>
  7.  
  8. int main(void)
  9. {
  10.     char *article = "";  // get plaintext from the user they would like to check. declaring char * here is telling main to expect a char type input & a its gonna be a pointer to an array of characters somewhere in memory (i dunno where tho remember how these work?)
  11.     do
  12.     {
  13.         article = get_string("Start typing dawg: ");
  14.     }
  15.     while (strlen(article) == 0);
  16.  
  17.     int letter = 0;    //  declare counting variable to count number of letters in plaintext article & set its value to 0
  18.     int word = 0;      //  declare counting variable to count number of words in plaintext article & set its value to 0
  19.     int sentence = 0;  //  declare counting variable to count number of sentances in plaintext article & set its value to 0
  20.  
  21.     int n = strlen(article);  // calculate stringlengh of entire article and store this in variable n
  22.  
  23.     for (int i = 0; i < n; i++) // forloop to loop through every character in the user's plaintext article
  24.     {
  25.         // count the number of letters in article
  26.         if (isalnum(article[i]))  // if the [i]th character being checked in the loop in the array of article is alphanumeric, add a count to the letters in the article
  27.         {
  28.             letter++;
  29.         }
  30.         // count the number of words in the article
  31.         if (i < n - 1 && isspace(article[i]) && isalnum(article[i + 1]))  // if i is less than the stringlengh of the article (minus the null char on the end of the string -1) AND the character at the ith index is a space AND the character in the spot of the array after is alphanumeric ([i + 1]), add a word to the count
  32.         {
  33.             word++;
  34.         }
  35.           // count the number of sentances in the article
  36.         if (i > 0 && (article[i] == '!' || article[i] == '.' || article[i] == '?') && isalnum(article[i - 1]))   // we start checking if i > 0 so we can start this check at the start of the plaintext. If the plaintext character at the index of the array being checked is a "!" "." or "?" AND the character in the index of the array is also alphanumeric BEFORE the index of the first special char [i] that was checked is being performed on [-1], add to the count of total sentences
  37.         {
  38.             sentence++;
  39.         }
  40.     }
  41.  
  42.     double grade = 0.0588 * (100 * letter / word) - 0.296 * (100 * sentence / word) - 15.8;  // calculate Coleman-Liau index. This is: grade = 0.0588 * (Letters per 100 words) - 0.296 * (Sentences per 100 words) - 15.8
  43.     printf("Letters: %i\nWords: %i\nSentences: %i\n", letter, word, sentence);  // show me letter, word and sentence count to make sure they are correct
  44.     printf("grade number %f: ", grade);  // show grade value that is spat out of coleman-liau index
  45.  
  46.     if (grade <= 1)  //  print result: Your reading score is
  47.     {
  48.         printf("u retarded.\n");
  49.     }
  50.     else if (grade < 16)
  51.     {
  52.         printf("grade %f\n", grade);
  53.     }
  54.     else
  55.     {
  56.         printf("Big brain niBBa.\n");
  57.     }
  58. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement