What am I doing wrong?!
When I check my code with check50, I get this:
:( handles questions in passage
expected "Grade 2\n", not "Grade 3\n"
So far, I haven't been able to fix this...tried Googling, changing the function for counting words several times...
Any help is appreciated!
My code below:
#include <cs50.h>
#include <ctype.h>
#include <math.h>
#include <stdio.h>
#include <string.h>
/* Prompt the user for some text
Count the number of letters, words, and sentences in the text
Compute the Coleman-Liau index: index = 0.0588 * L - 0.296 * S - 15.8
L is the average no of letters per 100 words in text; S is the average no of sentences per 100 words in text.
Print the grade level */
int countLetters(string text);
int countSentence(string text);
int countWord(string text);
int main(void)
{
string text = get_string("Enter your text: ");
int letter = countLetters(text);
int sentence = countSentence(text);
int word = countWord(text);
float average_letter = (float) letter / (float) word * 100;
float average_sentence = (float) sentence / (float) word * 100;
float index = (0.0588 * average_letter) - (0.296 * average_sentence) - 15.8;
int grade = ceil(index);
if (grade < 1)
{
printf("Before Grade 1\n");
}
else if (grade > 16)
{
printf("Grade 16+\n");
}
else
{
printf("Grade %i\n", grade);
}
return 0;
}
// function letters
int countLetters(string text)
{
int count_letter = 0;
for (int i = 0; text[i] != '\0'; i++)
{
if (isalpha(text[i]))
{
count_letter++;
}
}
return count_letter;
}
// function sentences
int countSentence(string text)
{
int count_sentence = 0;
int is_sentence = 0;
for (int i = 0; text[i] != '\0'; i++) {
char current_char = text[i];
if (current_char == '.' || current_char == '!' || current_char == '?' || current_char == ':')
{
if (is_sentence == 0)
{
is_sentence = 1;
count_sentence++;
}
}
else
{
is_sentence = 0;
}
}
return count_sentence;
}
// function words
int countWord(string text)
{
int count_word = 0;
int is_word = 0;
for (int i = 0; i < strlen(text); i++)
{
if (isalpha(text[i]))
{
if (i == 0 || !isalpha(text[i - 1]))
{
count_word++;
}
}
}
return count_word;
}