In this program, I need to find the reading grade level of a text inputted by the user.
The number of letters, words and sentences seem to be correct but theindex is coimg out wrong.
The assumptions given in the question:
a sentence:
will contain at least one word;
will not start or end with a space; and
will not have multiple spaces in a row.
Here is the code I wrote:
#include <ctype.h>
#include <cs50.h>
#include <math.h>
#include <stdio.h>
#include <string.h>
int count_letters(string text);
int count_words(string text);
int count_sentences(string text);
int main(void)
{
// Prompt the user for some text
string text = get_string("Text: ");
// Count the number of letters, words, and sentences in the text
float letters = count_letters(text);
float words = count_words(text);
float sentences = count_sentences(text);
// Compute the Coleman-Liau index
float L = (letters/words)*100;
float S = (sentences/words)*100;
float index = 0.0588 * L - 0.296 * S - 15.8;
int rounded_index = round(index);
printf("%fn", letters);
printf("%fn", words);
printf("%fn", sentences);
printf("%fn", L);
printf("%fn", S);
printf("%fn", index);
// Print the grade level
if (rounded_index<1)
printf("Before Grade 1n");
else if (rounded_index>=1 && rounded_index<16)
printf("Grade %in", rounded_index);
else
printf("Grade 16+n");
}
int count_letters(string text)
{
int lettercount = 0;
for (int i=0, length = strlen(text); i<length; i++)
{
if (isalpha(text[i])!=0)
{
lettercount++;
}
}
return lettercount;
}
int count_words(string text)
{
int wordcount = 1;
for (int i=0, length = strlen(text); i<length; i++)
{
if (text[i]==' ')
wordcount++;
}
}
return wordcount;
}
int count_sentences(string text)
{
int sentencecount = 0;
for (int i=0, length = strlen(text); i<length; i++)
{
if (text[i]=='.' || text[i]=='?' || text[i]=='!')
{
sentencecount++;
}
}
return sentencecount;
}
New contributor
user26471166 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.