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

int main(void)

{
    string text = get_string("Text: ");
    float letters = 0;
    float words = 0;
    float sentences = 0;
    
    for (int i = 0, n = strlen(&text[i]); i < n; i++)
    {
        if (isalpha(text[i]))
        {
            letters++;
        }
        
    }
    printf("%.1f letter(s)\n", letters);
    
    for (int i = 0, n = strlen(&text[i]); i < n; i++)
    {
        if (isspace(text[i]))
        {
            words++;
        }
    }
    printf("%.1f word(s)\n", words + 1);
    
    for (int i = 0, n = strlen(&text[i]); i < n; i++)
    {
        if (text[i] == '!' || text[i] == '.' || text[i] == '?')
        {
            sentences++;
        }
    }
    printf("%.1f sentence(s)\n", sentences);
    
    float L = (100.00 * letters) / words;
    float S = (100.00 * sentences) / words;
    float index = 0.0588 * L - 0.296 * S - 15.8;
    int grade = round(index);
    
    printf("Average letters per 100 words: %.1f\n", L);
    printf("Average sentences per 100 words: %.1f\n", S);
    printf("Grade %i\n", grade);
    
}

This is what I'm getting when using the following text

Text: In my younger and more vulnerable years my father gave me some advice that I've been turning over in my mind ever since.
96.0 letter(s)
23.0 word(s)
1.0 sentence(s)
Average letters per 100 words: 436.4
Average sentences per 100 words: 4.5
Grade 9

But it's suppose to give me Grade 7, and something around 417 letters per 100 words 4.3 sentences per 100 words

So I assume the grade issue results in an error in calculating the averages, but I can't figure out what.

I've also tried

float L = (letters / words) * 100;
float S = (sentences/ words) * 100; 

but no luck either.

1 Answer 1

0

You're right, the calculations are wrong. Mostly because one value, words, is wrong.

Note that when the code prints out the value of words,it adds 1 to the value of words first. But it doesn't change the value of words, so later, it's wrong.

printf("%.3f word(s)\n", words + 1);

BUT, in the calculations, the value of words is off by 1.

Does that tell you what's going on? It's the issue of how to count the last word.

If this answers your question, please click on the check mark to accept. Let's keep up on forum maintenance. ;-)

1
  • Oh my god, bless your soul that was it! Just one tiny change made all the difference. It's all fixed now. Thanks a bunch. Oct 28, 2020 at 3:25

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .