Created
March 26, 2020 22:55
-
-
Save ThatOneCubanDude/21aaaba2c3c933e5834759ec57305743 to your computer and use it in GitHub Desktop.
My solution to CS50x Problem Set 2 - Readability. Feel free to critique or ask questions.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <cs50.h> | |
#include <string.h> | |
#include <ctype.h> | |
#include <math.h> | |
int main(void) | |
{ | |
// gets text input from user | |
string text = get_string("Text: "); | |
// initiates variables to count letters, words, and sentences | |
float letters = 0; | |
float words = 1; | |
float sentences = 0; | |
for (int i = 0, n = strlen(text); i < n; i++) | |
{ | |
if (isalpha(text[i]) > 0) //if char at point i in text is a letter, adds 1 to letters | |
{ | |
letters++; | |
} | |
if (isspace(text[i]) > 0) //if char at point i in text is space, tab, etc., adds 1 to words | |
{ | |
words++; | |
} | |
if (text[i] == '.' || text[i] == '?' || text[i] == '!') //if char at point i in text is '.', '?', or '!' adds 1 to sentences | |
{ | |
sentences++; | |
} | |
} | |
//finds average letters per 100 words and sentences per 100 words | |
float L = ((100 / words) * letters); | |
float S = ((100 / words) * sentences); | |
//finds Coleman-Liau index | |
int index = round((0.0588 * L) - (0.296 * S) - 15.8); | |
//outputs the appropriate grade level based on index | |
if (index < 16) | |
{ | |
if (index < 1) | |
{ | |
printf("Before Grade 1\n"); | |
} | |
else | |
{ | |
printf("Grade %d\n", index); | |
} | |
} | |
else | |
{ | |
printf("Grade 16+\n"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment