mirror of https://github.com/me50/kukemuna.git
81 lines
1.7 KiB
C
81 lines
1.7 KiB
C
#include <cs50.h>
|
|
#include <ctype.h>
|
|
#include <math.h>
|
|
#include <stdio.h>
|
|
#include <string.h>
|
|
|
|
int get_letters(string text);
|
|
int get_words(string text);
|
|
int get_sentences(string text);
|
|
|
|
int main(void)
|
|
{
|
|
// Prompt the user for some text
|
|
string text = get_string("Text:\n");
|
|
|
|
// Count the number of letters, words, and sentences in the text
|
|
int letters = get_letters(text);
|
|
int words = get_words(text) + 1; // +1 for last word
|
|
int sentences = get_sentences(text);
|
|
|
|
// Compute the Coleman-Liau index
|
|
int index = round(0.0588 * (100 * letters / (float) words) - 0.296 * (100 * sentences / (float) words) - 15.8);
|
|
|
|
// Print the grade level
|
|
if (index < 1)
|
|
{
|
|
printf("Before Grade 1\n");
|
|
}
|
|
else if (index >= 16)
|
|
{
|
|
printf("Grade 16+\n");
|
|
}
|
|
else
|
|
{
|
|
printf("Grade %i\n", index);
|
|
}
|
|
|
|
// printf("# letters: %i\n", letters);
|
|
// printf("# words: %i\n", words);
|
|
// printf("# sentences: %i\n", sentences);
|
|
}
|
|
|
|
int get_letters(string text)
|
|
{
|
|
int letters = 0;
|
|
for (int i = 0, length = strlen(text); i < length; i++)
|
|
{
|
|
if (!isspace(text[i]) && !ispunct(text[i]))
|
|
{
|
|
letters++;
|
|
}
|
|
}
|
|
return letters;
|
|
}
|
|
|
|
int get_words(string text)
|
|
{
|
|
int words = 0;
|
|
for (int i = 0, length = strlen(text); i < length; i++)
|
|
{
|
|
if (isspace(text[i]))
|
|
{
|
|
words++;
|
|
}
|
|
}
|
|
return words;
|
|
}
|
|
|
|
int get_sentences(string text)
|
|
{
|
|
int sentences = 0;
|
|
for (int i = 0, length = strlen(text); i < length; i++)
|
|
{
|
|
if (text[i] == '!' || text[i] == '?' || text[i] == '.')
|
|
{
|
|
sentences++;
|
|
}
|
|
}
|
|
return sentences;
|
|
}
|