Skip to content

Instantly share code, notes, and snippets.

@voidnerd
Last active April 19, 2020 21:05
Show Gist options
  • Save voidnerd/d15209e7e3873ce36508b6e072087c73 to your computer and use it in GitHub Desktop.
Save voidnerd/d15209e7e3873ce36508b6e072087c73 to your computer and use it in GitHub Desktop.
CS50's Problem Set 2 - Readablity
#include <stdio.h>
#include <cs50.h>
#include <ctype.h>
#include <string.h>
#include <math.h>
int main(void)
{
string text = get_string("Text:");
int letterCount = 0;
int wordCount = 1;
int sentenceCount = 0;
for(int i = 0, len = strlen(text); i < len; i++)
{
if(isalpha(text[i]))
{
letterCount++;
}
else if (isspace(text[i]))
{
wordCount++;
}
else if(text[i] == '.' || text[i] == '!' || text[i] == '?')
{
sentenceCount++;
}
}
float L = (float)letterCount / (float) wordCount * 100;
float S = (float) sentenceCount / (float) wordCount * 100;
float index = 0.0588 * L - 0.296 * S - 15.8;
if (index < 1)
{
printf("Before Grade 1\n");
}
else if (index > 16)
{
printf("Grade 16+\n");
}
else
{
printf("Grade %i\n", (int) round(index));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment