I have an issue in the Readability part of the CS50 course that I've tried to troubleshoot forever, but I can't seem to get anywhere. I'm pretty sure the issue here is my index function that's not calculating it right or my l and s being an issue, but I'm lost at this point, and I'm hoping someone can help.
After having debugged multiple parts of the code, I've concluded that the issue must be in this part of the code:
//Calculate the average number of letters and sentences per 100 words
float l = (letter_count / word_count) * 100;
float s = (sentence_count / word_count) * 100;
//Calculating the Coleman-Liau index
int index = round(0.0588 * l - 0.296 * s - 15.8);
Full code snippet for context:
#include <cs50.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <math.h>
//Declaring all the functions
int count_letters(string text_input);
int count_words(string text_input);
int count_sentences(string text_input);
int main (void)
{
//Getting a string to measure
string text_input = get_string("Text: ");
//Calling all functions
int letter_count = count_letters(text_input);
int word_count = count_words(text_input);
int sentence_count = count_sentences(text_input);
//Calculate the average number of letters and sentences per 100 words
float l = (letter_count / word_count) * 100;
float s = (sentence_count / word_count) * 100;
//Calculating the Coleman-Liau index
int index = round(0.0588 * l - 0.296 * s - 15.8);
//Printing results
if (index < 1)
{
printf("Before Grade 1");
}
else if (index > 16)
{
printf("Grade 16+");
}
else
{
printf("Grade %i", index);
}
printf("\nIndex %i\n", index);
}
//Counting letters vai a loop
int count_letters(string text_input)
{
int letters = 0;
for (int i = 0, len = strlen(text_input); i < len; i++)
{
if (isalpha(text_input[i]))
{
letters++;
}
}
return letters;
}
//Counting words by counting spaces and adding 1 to the end.
int count_words(string text_input)
{
int words = 1;
for (int i = 0, len = strlen(text_input); i < len; i++)
{
if (text_input[i] == ' ')
{
words++;
}
}
return words;
}
//Counting number of '.', '?' and '!' to count sentences
int count_sentences(string text_input)
{
int sentences = 0;
for (int i = 0, len = strlen(text_input); i < len; i++)
{
if (text_input[i] == '.' || text_input[i] == '!' || text_input[i] == '?')
{
sentences++;
}
}
return sentences;
}