0

I have been testing my code with check 50 and one of the test cases - is calculated as Grade 9 in my program vs Grade 8.

https://submit.cs50.io/check50/1bffffe43d39fcfdc8d7a8885bbf59b47b718918

Test case input string - "Alice was beginning to get very tired of sitting by her sister on the bank, and of having nothing to do: once or twice she had peeped into the book her sister was reading, but it had no pictures or conversations in it, "and what is the use of a book," thought Alice "without pictures or conversation?"..."

I might be going wrong in multiple areas and am unable to debug if I am making a mistake calculating the number of characters/words or is it a case of innacuracy while converting different data types. I already checked my work several times and did the right casting to use a float throughout.

Here's my current code:

int main(int argc, string argv[]) { // Input String string s = get_string("Text: ");

//Count number of letters, words, sentences

int countofletters = 0;
int countofwords = 0;

//add +1 if the article starts with alphanumeric letter

if (isalpha(s[0]))
{
    countofwords = 1;
}

int countofsentances = 0;

for (int i = 0, length = strlen(s); i < length; i++)
{
    // Count Letters
    if (isalpha (s[i]))
    {
        countofletters++;
    }

    // Count words

   if ((i < (length - 1)) && isspace(s[i]) && isalpha(s[i + 1]))
    {
        countofwords++;
    }

    if (i > 0 && (s[i] == '!' || s[i] == '?' || s[i] == '.') && isalpha(s[i - 1]))
    {
        countofsentances++;
    }

}
printf("%i letter(s)\n", countofletters);
printf("%i word(s)\n", countofwords);
printf("%i sentances(s)\n", countofsentances);

//Index calculation

float indexf = 0;
// L is the average number of letters per 100 words in the text
float L = (float)(countofletters * 100) / countofwords;
// S is the average number of sentences per 100 words in the text
float S = (float)(countofsentances * 100) / countofwords;

indexf = 0.0588 * L - 0.296 * S - 15.8;

printf("%f indexf before rounding\n", indexf);

// Aproximate to nearest grade
// Cast float to int post rounding

int index;
index = (int) roundf(indexf);

printf("%i index \n", index);

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

}

1

Your word count, its output is wrong (It should be 57). Read the specifications again and stick to the boundaries that it is explicit being told. readability word-section

| improve this answer | |
  • Thanks a lot! I think the video misguided me a little bit. Removing the additional conditions and just checking for spaces fixed the problem. Much appreciated. – rajarshc Jan 8 at 3:00

You must log in to answer this question.

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