I've coded this solution for the readability exercise of CS50 Week 2, but after three long days I really don't see where I went wrong. It compiles and calculates the right number of words, sentences and letters, but its not getting the grade :c
#include <stdio.h>
#include <string.h>
#include <math.h>
int count_letters(string text);
int count_words(string text);
int count_sentences(string text);
int main(void)
{
// Get text
string text = get_string ("Text: ");
// Apply functions that measure letters, words and sentences
int letters = count_letters(text);
int words = count_words(text);
int sentences = count_sentences(text);
//Calculate parameters and apply formula
float L = 100.0 * (letters / words);
float S = 100.0 * (sentences / words);
int index = round(0.0588 * L - 0.296 * S -15.8);
//Show results
if (index > 16)
{
printf("Grade 16+\n");
}
else if (index < 1)
{
printf("Before grade 1\n");
}
else
{
printf("Grade %i\n", index);
}
}
//Count letters
int count_letters(string text)
{
int l = strlen(text);
int s = 0;
//For function to count through the string
for (int i = 0; i < l; i++)
{
//If to detect the spaces
if (text[i] == ' ')
{
s++;
}
}
//Spaces less total count
l -= s;
return l;
}
//Count words
int count_words(string text)
{
int wc = 1;
//Same logic as before, but we use the spaces (ASCII 32) to count a new word
for (int i = 0, l = strlen(text); i < l; i++)
{
if (text[i] == 32)
{
wc++;
}
}
return wc;
}
//Count sentences
int count_sentences(string text)
{
int wc = 0;
//Same logic as the words, but considering more parameters
for (int i = 0, l = strlen(text); i < l; i++)
{
if (text[i] == '.'|| text[i] == '?'||text[i] == '!')
{
wc++;
}
}
return wc;
} ```