0

I'm looking for some help with the readability problem from problem set 2. When I run debug50, I'm getting an error message for the text (see image). It seems that the body of the text is not being sent over to the various functions to count letters, words and sentences, which makes me think it's a scope issue? Any advice TIA!

enter image description here

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

int count_letters(string text);
int count_words(string text);
int count_sentences(string text);

int main(void)
{
    // prompt user for some text
    string text = get_string("Text: ");

    // count the number of letters, words and sentences in the text
    int letters = count_letters(text);
    int words = count_words(text);
    int sentences = count_sentences(text);

    // calculate variables L & S for Coleman-Liau index
    float L = (letters / words) * 100;
    float S = (sentences / words) * 100;

    // compute the Coleman-Liau index
    float CLindex = 0.0588 * L - 0.296 * S - 15.8;

    // round result
    int result = round(CLindex);

    // print the grade level
    if (result < 1)
    {
        printf("Before Grade 1\n");
    }
    else if (result == 1 || result <= 16)
    {
        printf("Grade %i\n", result);
    }
    else
    {
        printf("Grade 16+\n");
    }
}

int count_letters(string text)
{
    // check if characters are alphabetical and count them
    int letters = 0;
    for (int i = 0, j = strlen(text); i < j; i++)
    {
        if (isalpha(text[i]) == true)
        {
            letters++;
        }
    }
    return letters;
}

int count_words(string text)
{
    // count number of words
    int words = 1;
    for (int i = 0, j = strlen(text); i < j; i++)
    {
        if (isblank(text[i]) == true)
        {
            words++;
        }
    }
    return words;
}

int count_sentences(string text)
{
    // count number of sentences
    int sentences = 0;
    for (int i = 0, j = strlen(text); i < j; i++)
    {
        if (ispunct(text[i]) == true)
        {
            sentences++;
        }
    }
    return sentences;
}

1 Answer 1

0

Suggest set a breakpoint in get_letters at if (isalpha(text[i]) == true) and see what happens next. If it is not what you expect, review the man page for the char "is" functions, expecially the function signatures and the Return Value.

Another bug waiting to happen is ispunct(). According to the spec:

You can consider any sequence of characters that ends with a . or a ! or a ? to be a sentence.

ispunct will count commas and quotes and colons, oh my! as sentence delimiters. count_sentences should only consider the specified symbols. There is no function that will find only those symbols, so an explicit if (or ifs) is indicated.

8
  • Thank you for that! I was able to fix the problem in the 'count_letters', 'count_words' and 'count_sentences' functions, and I also figured out that I needed to cast the integers from lines 23 & 24 in floats (where I calculate the L & S variables for the Coleman-Liau index), but the results are not correct, which makes me think I'm calculating the variables L and S wrong. Can you give me any tips for how to figure this out? Thank you!
    – AmyG
    Commented Jan 20 at 15:17
  • Curious how you fixed count_sentences. What does the sentence One fish; two fish. Red fish; blue fish" return from that function? Commented Jan 20 at 20:18
  • I was using the 'ispunct' function wrong, I changed it so it just says: if (ispunct(text[i])). The output for the excerpt "One fish. Two fish. Red fish. Blue fish" is 4.
    – AmyG
    Commented Jan 20 at 22:34
  • I've been trying a few different ways to calculate the L & S variables for the Coleman-Liau index but still no luck. Any tips you can give me would be greatly appreciated! My latest attempt was: float L = ((100 / (float)words) * ((float)letters)) / 100; float S = (((float)sentences) * (100 / (float)words)) / 100;
    – AmyG
    Commented Jan 20 at 22:56
  • Read this again (maybe copy/paste). "One fish; two fish. Red fish; blue fish". I am questioning the use of ispunct. , , " , ; are all punctuation, no? Commented Jan 21 at 0:39

You must log in to answer this question.

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