1

I did Readability problem set but I feel it is wrong to initialize words to 1 but it is the only way to satisfy the requirement since the first the words are counted by space so the last word can not be counted I added one automatically just in case, so the question is what do your think guys about it ? and what did you do to solve it.

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

 int main(void)
{ 
int sum_letters = 0 , sum_sent = 0 ;
float words = 1 ;

char* text =  get_string("Enter your Text :");

for (int i = 0 ; text[i] != 0x00 ; i++)
{
    if (isalpha(text[i]))
    {
        sum_letters ++ ;
    }
    else
    if (isspace(text[i]))
    {
        words ++;
    }
    else
    if (text[i] == '.' || text[i] =='!' || text[i] =='?') //isalnum(text[i]) == false)
    {
        sum_sent ++;
    }
}

int L = (sum_letters / words * 100); // Letters average = letters / words * 100
float S = (sum_sent / words * 100); // Sentence average  = sentence / words * 100

float index = (0.0588 * L - 0.296 * S - 15.8) ;

printf("%i letter(s)\n", sum_letters);
printf("%.f word(s)\n", words);
printf("%i sentance(s)\n", sum_sent);

if (index >= 16) // determining the output
{
    printf("Grade 16+\n");
}
else
if (index < 1)
{
    printf("Before Grade 1\n");
}
else
printf("Grade %.f\n", round(index));

}

enter image description here

1
  • Idk bro seems alright to me because I did the same hehehe – Ojou Nii Apr 13 '20 at 18:44
0

What happens when it reaches the last word? how many words and spaces are in a sentence?

0

I did mine the same way

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

int main(void)
{
    string text = get_string("Text: ");
    int letterscount = 0;
    int wordcount = 1;
    int sentencecount = 0;

//count words
    for (int i = 0; i < strlen(text); i++)
    {
       if ((text[i] >= 'a' && text[i] <= 'z') || (text[i] >= 'A' && text[i] <= 'Z'))
       {
           letterscount++;
       }
       else if (text[i] == ' ')
       {
           wordcount++;
       }
       else if (text[i] == '.' || text[i] == '!' || text[i] == '?')
       {
           sentencecount++;
       }
    }
//    printf("letters: %i; words: %i; sentences: %i\n", letterscount, wordcount, sentencecount);

    float grade = 0.0588 * (100 * (float) letterscount / (float) wordcount) - 0.296 * (100 * (float) sentencecount / (float) wordcount) - 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");
    }

}

You must log in to answer this question.

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