Hi guys, I can not find bugs in my code please help
Thank you in advance!!!
This programes is to computes the approximate grade level needed to comprehend some text, per the below.
$ ./readability
Text: Congratulations! Today is your day. Youâre off to Great Places! Youâre off and away!
Grade 3
And I stuck at the following two test text:
first text I should get Grade 7 but output is Grade 8,
second text should be Grade 8 but output is Grade 9.
The text used to test is:
Grade 7 : In my younger and more vulnerable years my father gave me some advice that Iâve been turning over in my mind ever since.
Grade 8 : Alice was beginning to get very tired of sitting by her sister on the bank, and of having nothing to do: once or twice she had peeped into the book her sister was reading, but it had no pictures or conversations in it, âand what is the use of a book,â thought Alice âwithout pictures or conversation?â
#include <stdio.h>
#include <cs50.h>
#include <string.h>
#include <ctype.h>
#include <math.h>
int countLetters(string text);
int countWords(string text);
int countSentences(string text);
int main(void)
{
string s = get_string("Text: ");
int numberOfLetters = countLetters(s);
int numberOfWords = countWords(s);
int numberOfSentences = countSentences(s);
float L = 100 * numberOfLetters / numberOfWords;
float S = 100 * numberOfSentences / numberOfWords;
int index = round(0.0588 * L - 0.296 * S - 15.8);
if (index < 1)
{
printf("Before Grade 1");
}
else if (index >= 16)
{
printf("Grade 16+");
}
else
{
printf("Grade %i", index);
}
}
int countLetters(string text)
{
int numberOfLetters = 0 ;
for (int i = 0 ; text[i] != '\0' ; i++)
{
bool isCurrentCharAnAlpha = isalpha(text[i]);
if (isCurrentCharAnAlpha)
{
numberOfLetters++;
}
}
return numberOfLetters;
}
int countWords(string text)
{
int numberOfWords = 0 ;
for (int i = 0 ; text[i] != '\0' ; i++)
{
bool isCurrentCharAnAlpha = isalpha(text[i]);
if (isCurrentCharAnAlpha)
{
if (i == 0)
{
numberOfWords++;
}
else if (text[i - 1] == ' ')
{
numberOfWords++;
}
}
}
return numberOfWords;
}
int countSentences(string text)
{
int numberOfSentences = 0 ;
for (int i = 0 ; text[i] != '\0' ; i++)
{
if (text[i] == '.' || text[i] == '!' || text[i] == '?')
{
numberOfSentences++;
}
}
return numberOfSentences;
}