Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <math.h>
- #include <cs50.h>
- #include <ctype.h>
- #include <string.h>
- int main(void)
- {
- 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?)
- do
- {
- article = get_string("Start typing dawg: ");
- }
- while (strlen(article) == 0);
- int letter = 0; // declare counting variable to count number of letters in plaintext article & set its value to 0
- int word = 0; // declare counting variable to count number of words in plaintext article & set its value to 0
- int sentence = 0; // declare counting variable to count number of sentances in plaintext article & set its value to 0
- int n = strlen(article); // calculate stringlengh of entire article and store this in variable n
- for (int i = 0; i < n; i++) // forloop to loop through every character in the user's plaintext article
- {
- // count the number of letters in article
- 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
- {
- letter++;
- }
- // count the number of words in the article
- 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
- {
- word++;
- }
- // count the number of sentances in the article
- 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
- {
- sentence++;
- }
- }
- 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
- printf("Letters: %i\nWords: %i\nSentences: %i\n", letter, word, sentence); // show me letter, word and sentence count to make sure they are correct
- printf("grade number %f: ", grade); // show grade value that is spat out of coleman-liau index
- if (grade <= 1) // print result: Your reading score is
- {
- printf("u retarded.\n");
- }
- else if (grade < 16)
- {
- printf("grade %f\n", grade);
- }
- else
- {
- printf("Big brain niBBa.\n");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement