0

I am not sure what I am doing wrong.

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

int main(void)
{
    //Create integers counters
    int letters = 0;
    int words = 1;
    int sentences = 0;

    //Ask for text
    string txt = get_string("Text: \n");

    int txtLength = strlen(txt);

    // Loop into the string
    for (int i = 0; i < txtLength; i++)
    {
        if (isalpha(txt[i]))
        {
            letters++;
        }
        if (isblank(txt[i]))
        {
            words++;
        }
        if (ispunct(txt[i]))
        {
            sentences++;
        }
    }

    // Calcule Coleman-Liau
    float L = ((float) letters / (float) words * 100.0);
    float S = ((float) sentences / (float) words * 100.0);
    float index = (0.0588 * L - 0.296 * S - 15.8);

    int grade = round(index);

    //Checking grade results
    if (grade >= 16)
    {
        printf("Grade 16+\n");
    }
    else if (grade < 1)
    {
        printf("Before Grade 1\n");
    }
    else
    {
        printf("Grade %i\n", grade);
    }

}

My result:

:) readability.c exists
:) readability.c compiles
:( handles single sentence with multiple words
    expected "Grade 7\n", not "Grade 6\n"
:( handles punctuation within a single sentence
    expected "Grade 9\n", not "Grade 5\n"
:( handles more complex single sentence
    expected "Grade 8\n", not "Grade 3\n"
:( handles multiple sentences
    expected "Grade 5\n", not "Grade 2\n"
:( handles multiple more complex sentences
    expected "Grade 10\n", not "Grade 8\n"
:( handles longer passages
    expected "Grade 8\n", not "Grade 4\n"
:) handles questions in passage
:) handles reading level before Grade 1
:( handles reading level at Grade 16+
    expected "Grade 16+\n", not "Grade 13\n"

1 Answer 1

1

From man ispunct:

ispunct()

checks for any printable character which is not a space or an alphanumeric character.

This if (ispunct(txt[i])) will be true for a whole host of characters that are not ., ? or !.

1
  • Thank you for the heads up! I compared those characters instead of the whole list of characters and it worked well. Commented Jul 6, 2020 at 14:02

You must log in to answer this question.

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