54 lines
1.4 KiB
C
54 lines
1.4 KiB
C
|
#include <cs50.h>
|
||
|
#include <stdio.h>
|
||
|
#include <string.h>
|
||
|
#include <math.h>
|
||
|
|
||
|
int main(void)
|
||
|
{
|
||
|
//setting initial counters, words start at 1 due to last word not having space behind.
|
||
|
long letters = 0;
|
||
|
long words = 1;
|
||
|
long sentences = 0;
|
||
|
|
||
|
//getting string from user and looping through each character
|
||
|
string user_input = get_string("Text:");
|
||
|
for (int i = 0, n = strlen(user_input); i < n; i++)
|
||
|
{
|
||
|
//counting letters based on ascii values from a-z & A-Z
|
||
|
int ascii_code = (int) user_input[i];
|
||
|
if (ascii_code >= 65 && ascii_code <= 122)
|
||
|
{
|
||
|
letters++;
|
||
|
}
|
||
|
// counting words based on ascii value for space
|
||
|
else if (ascii_code == 32)
|
||
|
{
|
||
|
words++;
|
||
|
}
|
||
|
//coounting sentences based on ascii value for ".","!","?"
|
||
|
else if (ascii_code == 33 || ascii_code == 63 || ascii_code == 46)
|
||
|
{
|
||
|
sentences++;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
//calculating Coleman-Liau index
|
||
|
float l = (float)letters / words * 100;
|
||
|
float s = (float)sentences / words * 100;
|
||
|
long index = round(0.0588 * l - 0.296 * s - 15.8);
|
||
|
|
||
|
//writing out readabality grade based on index value
|
||
|
if (index < 1)
|
||
|
{
|
||
|
printf("Before Grade 1\n");
|
||
|
}
|
||
|
else if (index >= 16)
|
||
|
{
|
||
|
printf("Grade 16+\n");
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
printf("Grade %li\n", index);
|
||
|
}
|
||
|
|
||
|
}
|