Skip to content

Instantly share code, notes, and snippets.

@edieblu
Created May 5, 2021 18:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save edieblu/05dda72a1fbbeb37835c531ea55ec658 to your computer and use it in GitHub Desktop.
Save edieblu/05dda72a1fbbeb37835c531ea55ec658 to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <cs50.h>
#include <math.h>
#include <string.h>
#include <ctype.h>
// Coleman-Liau index
// L (avg num of letters/100 words)
// S (avg num of sentences/100 words)
// a letter is any uppercase or lowercase alphabetic character
// a word is a character separated by spaces
// period, exclamation point, or question mark indicates the end of a sentence
// isalpha - check whether a character is alphabetical
int main(void)
{
int sentences = 0, words = 1, letters = 0;
float L, S, grade;
string text = get_string("Text:\n");
for (int i = 0, len = strlen(text); i < len; i++)
{
if (isalpha(text[i]))
{
letters++;
}
else if (text[i] == ' ')
{
words++;
}
if (text[i] == '.' || text[i] == '!' || text[i] == '?')
{
sentences++;
}
}
// printf("Letters: %i\n", letters);
// printf("Words: %i\n", words);
// printf("Sentences: %i\n", sentences);
// round result to the nearest whole number
L = (float)letters * 100 / (float)words;
S = (float)sentences * 100 / (float)words;
grade = round(0.0588 * L - 0.296 * S - 15.8);
if (grade > 16)
{
printf("Grade 16+\n");
}
else if (grade < 1)
{
printf("Before Grade 1\n");
}
else
{
printf("Grade %i\n", (int)round(grade));
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment