Readability CS50, why am I facing this issue?

//INSTRUCTIONS
//Readability should take some text and assess the approximate reading level of that text
//So, when I run the program, my program should prompt the user to type in some text
//After the user types in the text he wants to, my program should give an output of the approximate U.S. Grade reading level

//OBSERVATIONS TO NOTE
//Longer words generally means a greater reading level
//More words in a sentence also generally represents a greater reading level

//CALCULATE THE READING LEVEL
//Coleman Liau Index = index = 0.0588 * L - 0.296 * S - 15.8
//L = Average number of letters per 100 words.
//S = Average number of sentences per 100 words
//Calculate the number of words, letters and sentences and use the Coleman Liau Formula to figure out what grade level is most appropriate
//Determine the total number of letters in the formula, in this case firstly the numbers of the Uppercase and the Lowercase
//Loop through each letter
//Assess whether a thing is a letter or not
//A word in this challenge is going to be any sequence of characters that is seperated by a space
//Loop through the string looking for the number of spaces, there's a space after each word except the last one
//For this problem, any period, exclamation point, or question mark indicates the end of a sentence
//Loop through the sentences and check how many periods, exclamation points and question marks we see
//Take 3 values, letters, words and sentences, and plug them into the coleman layout index formula
//Round up the values to what the reading level really is to the nearest whole number
//Output should be Grade X where X corresponds to the reading level
//Set a minimum or maximum, if output is less than 1, then output "Before Grade 1" and if it is more than 16, then output "Grade 16+"

//WHAT I SHOULD REMEMBER -
// I can treat a string as an array of characters
// Don't forget to include the required libraries

//LINKS I MIGHT WANT TO USE - https://manual.cs50.io/

#include <stdio.h>
#include <cs50.h>
#include <string.h>
#include <ctype.h>
#include <math.h>

int main(void)
{
    //Prompting the user for an Input
    string text = get_string("Text: ");
     //Declaring the words, letters and sentences which we will use to keep count
    int L = 0 , S = 0 , words = 0, sentences = 0 , letters = 0;
    char w;
    float g = 0;
    double LI;
    // Looping through the words, letters and sentences
    for (int i = 0, length = strlen(text); i < length; i++)
    {
        w = text[i];
        printf("%c", w);


     if ((w >= 'a' && w <= 'z') || (w >= 'A' && w <= 'Z'))
     {
         letters++;
     }
     else if (w == ' ')
     {
         words++;
     }
     else if (w == '!' || w == '?' || w == '.')
     {
         sentences++;
     }

    }

    // printf("\nNo of Letters: %i\n", letters);
    // printf("\nNo of Words: %i\n", words);
    // printf("\nNo of Sentences: %i\n", sentences);
    L = (100.00 * (letters/(float)words));
    S = (100.00 * (sentences/(float)words));
    LI = 0.0588 * L - 0.296 * S - 15.8;

    // printf("\nAverage No of Letters per 100 words: %i\n", (letters*100/words));
    // printf("\nAverage No of Sentences per 100 words: %i\n", (sentences*100/words));
    //printf("\nGrade %i\n",(int) round(LI));
        if (LI < 16 && LI >= 0)
    {
        printf("\nGrade %i\n", (int) round(LI));
    }
    else if (LI >= 16)
    {
        printf("\nGrade 16+\n");
    }
    else
    {
        printf("\nBefore Grade 1\n");
    }

The sentences “In my younger and more vulnerable years my father gave me some advice that I’ve been turning over in my mind ever since.” and “Congratulations! Today is your day. You’re off to Great Places! You’re off and away!” are not accurately displaying the result, they should be grade 3 and 7 respectively, but it tells me grade 5 and 9, most of the other sentences I’ve tried are very accurate. I feel like it’s an issue of either rounding off or something with the formula.

I hope I’m allowed to ask CS50 related doubts?

doing this it doesn’t seem as if you are counting the last word in a string
try to manually count the words and see if it matches

2 Likes

You are totally allowed to ask cs50 questions here.

Cs50 has plenty of active and supportive communities like FCC.
So you can post your questions there as well.