1

I tried using debugger but could not figure out where I went wrong. please help

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

int main(void)
{
    int letters = 0, words = 0, sentences = 0; 
    string text = get_string("Enter text:\n");
    for (int i = 0 ; text[i] != '\0'; i++)  //Loop to count letters words and sentences
    {
        if ((text[i] >= 'a' && text[i] <= 'z') || (text[i] >= 'A' && text[i] <= 'Z'))
        {
            letters++;
        }
        
        else if (text[i] == ' ' || text[i] == '\n' || text[i] == '\t')
        {
            words++;
        }
        else if (text[i] == '.' || text[i] == '!' || text[i] == '?')
        {
            sentences++;
        }
    }
    printf("Letters: %i\nWords: %i\nSentences: %i\n", letters, words, sentences);
    
    float L = ((float) letters / (float) words) * 100;
    printf("Average No. of letters: %f\n", L);
    float S = ((float) sentences / (float) words) * 100;
    printf("Average No. of sentences: %f\n", S);
    float grade = (0.0588 * L) - (0.296 * S) - 15.8;
    if (grade <= 16 && grade >= 0)
    {
        printf("Grade %i\n", (int) round(grade));
    }
    else if (grade > 16)
    {
        printf("Grade 16+\n");
    }
    else
    {
        printf("Before Grade 1\n");
    }

}

enter image description here

2 Answers 2

0

This is a good question and a case where very specific test data would quickly lead to the problem and a solution. It's a good demonstration why test data must be carefully thought out and tested! ;-)

One big clue is that the word count is always low by 1. Since you've said that you tried to figure it out using debugger, I'll give you credit for that. (So many people on this forum don't run their programs in the debugger and it's obvious.) This problem is doing a good job of hiding. I won't tell you what the problem is, but I'll point you in the right direction!

Is the LAST word being counted? What happens when the text ends with a period? Is the result different if the period is followed by a space instead of hitting the return key? What are the TWO chars that come at the end of every sentence (or should I say, between the end of one sentence and the start of the next.) How is that different with the end of the text?

Note that I'm ignoring the calculation errors (appear to be corrected in your edit), because if the raw counts aren't accurate, the calculations can't be debugged.

If this doesn't help, leave a comment and we'll revisit it. ;-)

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

8
  • Between sentences ? Is it a new tab '\t' or so ? I edited my code a bit including tab. Please do check.
    – MerlinMR
    Commented Jul 10, 2020 at 14:11
  • I have also included the errors I got.
    – MerlinMR
    Commented Jul 10, 2020 at 14:22
  • Between sentences, there's a period (or question mark or eclamation point) and a SPACE. At the end of the text, there's normally no space. So, is the code accounting for the lack of a space at the end of the text when counting the last word?
    – Cliff B
    Commented Jul 10, 2020 at 17:53
  • Nope , its not.
    – MerlinMR
    Commented Jul 11, 2020 at 8:07
  • Well, there's your answer!
    – Cliff B
    Commented Jul 11, 2020 at 8:15
0

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

int main(void)

{

char text[1000];
char* fgets(char *text, int length, FILE * stream);
puts("Enter text:");
fgets(text,sizeof(text),stdin);

int letters =  0, words = 0, sentences = 0, n = strlen(text);

//Loop to count letters words and sentences

for (int i = 0 ; i < n ; i ++)
{
    if (isalpha(text[i]))
    {
        letters++;
    }
    else if (text[i] == ' ' && (text[i - 1] != '!' && text[i - 1] != '?' && text[i - 1] != '.'))
    {
        words++;
    }
    else if (text[i] == '.' || text[i] == '!' || text[i] == '?')
    {
        sentences++;
        words++;
    }
}

printf("\nLetters: %i\nWords: %i\nSentences: %i\n", letters, words, sentences);

// Coleman-Liau index computation

float L = (letters / (float) words) * 100;
printf("Average No. of letters: %f\n", L);
float S = (sentences / (float) words) * 100;
printf("Average No. of sentences: %f\n", S);
float index = round(0.0588 * L - 0.296 * S - 15.8);

if (index < 1)
{
    printf("Before Grade 1\n");
}
else if (index >= 16)
{
    printf("Grade 16+\n");
}
else
{
    printf("Grade %i\n", (int) index);
}

}

You must log in to answer this question.

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