0

This is the code for readability problem set 2. The code is working but not properly as it's failing test cases. the output grade is either +-1 from the expected result. Is there something wrong while rounding off?

#include <stdio.h>
#include <cs50.h>
#include <string.h>
#include <math.h>
#include <ctype.h>
int main(void)
{
    int l=0;
    int w=0;
    int i=0;
    int p=0;


    string sent = get_string("Please input Sentence:");
/*
    program should count-
    *letters - letters in words
    *words - seperated by spaces
    *sentences - ending with !?.
*/

    for (i=0; i < strlen(sent); i++) //Calculating Alphabets
    {
        if ( isalpha(sent[i]))
        {
            l+=1;
        }
        else
        {
            l+=0;
        }
    }

    for(i = 0; i < strlen(sent); i++) //calculating words as words are always seperated with space
    {
        if( isspace(sent[i]) )
        {
            w+=1;
        }
        else
        {
            w+=0;
        }
    }

    for(i = 0; i < strlen(sent); i++) //calculating sentences with punctuations || sent[i] =='""'
    {
        if( sent[i] =='.' || sent[i] == '?' || sent[i] =='!'  || sent[i] == ':'|| sent[i] == ';' )
        {
            p+=1;
        }
        else
        {
            p+=0;
        }
    }

These are the test cases:

:) readability.c exists
:) readability.c compiles
:( handles single sentence with multiple words
    expected "Grade 7\n", not "Grade 8\n"
:( handles punctuation within a single sentence
    expected "Grade 9\n", not "Grade 10\n"
:( handles more complex single sentence
    expected "Grade 8\n", not "Grade 7\n"
:( handles multiple sentences
    expected "Grade 5\n", not "Grade 4\n"
:) handles multiple more complex sentences
:( handles longer passages
    expected "Grade 8\n", not "Grade 7\n"
:( handles questions in passage
    expected "Grade 2\n", not "Grade 3\n"
:( handles reading level before Grade 1
    expected "Before Grade 1...", not "Before Grade 1..."
:) handles reading level at Grade 16+
To see the results in your browser go to https://submit.cs50.io/check50/0e0dfe8e8aaacbb333443476643f33dd09754350
1
  • Can't tell what the problem is from the posted code. But, here are some programming tips. The else clauses can all be deleted. They serve no purpose. Adding 0 to a number is a pointless waste of processing cycles. Else clauses are purely optional. Next, there are 3 for loops when there only needs to be one. All 3 if statements can be put inside the same for loop. Now, if you'll edit your question and post all the relevant code, maybe we can find the problem.
    – Cliff B
    Sep 20 '21 at 22:45

You must log in to answer this question.

Browse other questions tagged .