Skip to content

Instantly share code, notes, and snippets.

@teeschorle
Last active October 22, 2022 18:32
Show Gist options
  • Save teeschorle/78f9b046b190436b42b8818dcbaf8a5e to your computer and use it in GitHub Desktop.
Save teeschorle/78f9b046b190436b42b8818dcbaf8a5e to your computer and use it in GitHub Desktop.
CS50 Problem Set 2 (Fall 2019) - Readability
//CS50 Problem Set 2 (Fall 2019): Readability
//Author: teeschorle
#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");
}
}
@NaaAbbey
Copy link

I no longer know what to do
Can someone please help
check50

@danklad
Copy link

danklad commented Oct 11, 2020

Smoothest program ever!!!

@atifali10
Copy link

hey guys! can someone help me understand what did i do wrong? Is there something wrong with the logic?
Untitled

@divine3d
Copy link

hey guys! can someone help me understand what did i do wrong? Is there something wrong with the logic?
Untitled

Why don't you try using the isalnum() and isspace() for finding out letters and words respectively? Much intuitive and easier on the eyes.

 if (isalnum(para[i]))
        {
            letters++;
        }
        if (isspace(para[i]))
        {
            words++;
        }
        if (para[i] == '.' || para[i] == '!' || para[i] == '?')
        {
            sentences++;
        }

Just a suggestion. Thanks.

@atifali10
Copy link

hey guys! can someone help me understand what did i do wrong? Is there something wrong with the logic?
Untitled

Why don't you try using the isalnum() and isspace() for finding out letters and words respectively? Much intuitive and easier on the eyes.

 if (isalnum(para[i]))
        {
            letters++;
        }
        if (isspace(para[i]))
        {
            words++;
        }
        if (para[i] == '.' || para[i] == '!' || para[i] == '?')
        {
            sentences++;
        }

Just a suggestion. Thanks.

yeah! i found out about that after writing my code but felt too lazy to change it XD. But hey! it still works.
And i',m sorry i didn't clarify my question. This code compiles but i'm not getting the desired output. It's counting no. of letters, words, and sentences perfectly but its not registering the values correctly of float S and float L at line 26 and 27, where 'S' is avg no. of sentences(s) per 100 words(w) and 'L' is avg no. of letters(l) per 100 words(w). i found out about it when i used debug50 tool. So, is there something wrong with the logic i used at that point?

@divine3d
Copy link

hey guys! can someone help me understand what did i do wrong? Is there something wrong with the logic?
Untitled

Why don't you try using the isalnum() and isspace() for finding out letters and words respectively? Much intuitive and easier on the eyes.

 if (isalnum(para[i]))
        {
            letters++;
        }
        if (isspace(para[i]))
        {
            words++;
        }
        if (para[i] == '.' || para[i] == '!' || para[i] == '?')
        {
            sentences++;
        }

Just a suggestion. Thanks.

yeah! i found out about that after writing my code but felt too lazy to change it XD. But hey! it still works.
And i',m sorry i didn't clarify my question. This code compiles but i'm not getting the desired output. It's counting no. of letters, words, and sentences perfectly but its not registering the values correctly of float S and float L at line 26 and 27, where 'S' is avg no. of sentences(s) per 100 words(w) and 'L' is avg no. of letters(l) per 100 words(w). i found out about it when i used debug50 tool. So, is there something wrong with the logic i used at that point?

Have you tried casting those integers as floats. I guess you have declared w, s as int and integer division will yield integer outputs.
Like this:
float S = ((float) s / (float) w) * 100;

@atifali10
Copy link

Hey thanks man! That worked. So, if i divide integers, even though the resulting answer is in decimal, will it round of to the nearest integers as i declared those values as int?

hey guys! can someone help me understand what did i do wrong? Is there something wrong with the logic?
Untitled

Why don't you try using the isalnum() and isspace() for finding out letters and words respectively? Much intuitive and easier on the eyes.

 if (isalnum(para[i]))
        {
            letters++;
        }
        if (isspace(para[i]))
        {
            words++;
        }
        if (para[i] == '.' || para[i] == '!' || para[i] == '?')
        {
            sentences++;
        }

Just a suggestion. Thanks.

yeah! i found out about that after writing my code but felt too lazy to change it XD. But hey! it still works.
And i',m sorry i didn't clarify my question. This code compiles but i'm not getting the desired output. It's counting no. of letters, words, and sentences perfectly but its not registering the values correctly of float S and float L at line 26 and 27, where 'S' is avg no. of sentences(s) per 100 words(w) and 'L' is avg no. of letters(l) per 100 words(w). i found out about it when i used debug50 tool. So, is there something wrong with the logic i used at that point?

Have you tried casting those integers as floats. I guess you have declared w, s as int and integer division will yield integer outputs.
Like this:
float S = ((float) s / (float) w) * 100;

Hey thanks man! That worked. So, if i divide integers, even though the resulting answer is in decimal, will it round of to the nearest integers as i declared those values as int?

@divine3d
Copy link

Hey thanks man! That worked. So, if i divide integers, even though the resulting answer is in decimal, will it round of to the nearest integers as i declared those values as int?

hey guys! can someone help me understand what did i do wrong? Is there something wrong with the logic?
Untitled

Why don't you try using the isalnum() and isspace() for finding out letters and words respectively? Much intuitive and easier on the eyes.

 if (isalnum(para[i]))
        {
            letters++;
        }
        if (isspace(para[i]))
        {
            words++;
        }
        if (para[i] == '.' || para[i] == '!' || para[i] == '?')
        {
            sentences++;
        }

Just a suggestion. Thanks.

yeah! i found out about that after writing my code but felt too lazy to change it XD. But hey! it still works.
And i',m sorry i didn't clarify my question. This code compiles but i'm not getting the desired output. It's counting no. of letters, words, and sentences perfectly but its not registering the values correctly of float S and float L at line 26 and 27, where 'S' is avg no. of sentences(s) per 100 words(w) and 'L' is avg no. of letters(l) per 100 words(w). i found out about it when i used debug50 tool. So, is there something wrong with the logic i used at that point?

Have you tried casting those integers as floats. I guess you have declared w, s as int and integer division will yield integer outputs.
Like this:
float S = ((float) s / (float) w) * 100;

Hey thanks man! That worked. So, if i divide integers, even though the resulting answer is in decimal, will it round of to the nearest integers as i declared those values as int?

Yes. Any math operation on Integers will yield an integer unless at least one of them is a float!
Glad it helped!

@lillianassefa
Copy link

I am confused on line 9, why are there 2 brackets?
Amazing work!! And also thanks in advance.

@ys-cao
Copy link

ys-cao commented Nov 15, 2020

Why do you assign wordcount equal to 1?
How to include sequence separated by one or more spaces to wordcount?

Because at the end of the last word in the text, there is no space. You would need to add 1 manually to count the last word.

@arl4195
Copy link

arl4195 commented Nov 17, 2020

This was a big help in understanding the sequence of events, such as the else/if statements. I put several several lines in reverse order and it compiled but did not pass the check test. After reviewing your code I realized the mistakes. Thanks

@ys-cao
Copy link

ys-cao commented Nov 17, 2020

This was a big help in understanding the sequence of events, such as the else/if statements. I put several several lines in reverse order and it compiled but did not pass the check test. After reviewing your code I realized the mistakes. Thanks

I'm glad that I could help.

@MatchYouPikchu
Copy link

MatchYouPikchu commented Nov 24, 2020

Hello guys - a question, did the cs 50 actually provided the wrong assumption in the course, which messes up actually my count of words. It was said quote "You may assume that a sentence will not start or end with a space, and you may assume that a sentence will not have multiple spaces in a row."
But when checking actual content in the automated tests it DOES HAVE sentences starting with space. Oh, not cool man, I think I do have wrong results because of that. I guess there needs to be a function which people pasted beforehand about clearing those nasty bastards.

@M-Ali-Horya
Copy link

M-Ali-Horya commented Feb 27, 2021

Kindly any one can help me ! I do not know what is the problem here:

@M-Ali-Horya
Copy link

image

@M-Ali-Horya
Copy link

image

@mossburger55
Copy link

If I am not mistaken I think week 2 lecture talked about the need of declaring the size of an array.
I don't know if or how to declare the size in this problem.

@Julzar21
Copy link

I added wordcount++ at the end of my for loop coz at the end of the text is (".") so the code wont read it to add another word but a sentence

@sujansuja
Copy link

Why do you assign wordcount equal to 1? How to include sequence separated by one or more spaces to wordcount?

Because the last word will not be counted since it is not having space after it. So

@naaomiwang
Copy link

I'm quite confused about Line 19. How would the computer be able to determine letters larger than 'a' or smaller than 'z', or will it automatically translate to ACSII? Thanks :)

@sujansuja
Copy link

@General-99
Copy link

hi guys.....why does int count_words have to be 1 and count_letter and count_words is 0????

@renegadec
Copy link

Hey, so Word count is starting when the program sees a space using this:

else if (text[i] == ' ') { wordcount++; }

Remember, the word before the first space will not be counted so you have to start from 1.

@ayshkhndlwl
Copy link

ayshkhndlwl commented Jun 23, 2022

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

int main(void)
{
int i;
string text;
do
{
text = get_string("Text: ");
}
while (text[0] == '\n' || text[0] == ' ' || text[0] == '\0' || ispunct(text[0]));

int len = strlen(text);
float letters = 0, words = 1, sentences = 0;

for (i = 0; i < len; i++)
{
    // count letters
    if (isalnum(text[i]))
    {
        letters++;
    }

    // count words
    else if (isspace(text[i]) && text[i + 1] != '\0')
    {
        words++;
    }

    // count sentences
    else if (text[i] == '.' || text[i] == '?' || text[i] == '!')
    {
        sentences++;
    }
}

float index, L, S;
// evaluate average number of letters per 100 words
L = (letters / words) * 100;

// evaluate average number of sentences per 100 words
S = (sentences / words) * 100;

// evaluate the Coleman-Liau Index
index = 0.0588 * L - 0.296 * S - 15.8;

/* printf("Letters: %.2f\nWords: %.2f\nSentences: %.2f\nGrade %.0f\n", letters, words, sentences, round(index)); */

// for index value less than 1, print "Before Grade 1"
if (index < 1)
{
    printf("Before Grade 1\n");
}

// for index value more than 16, print "Grade 16+"
else if (index > 16)
{
    printf("Grade 16+\n");
}

// if neither of the above cases, print the index value obtained
else
{
    printf("Grade %.0f\n", index);
}

}

Here's my code for this. Would appreciate any comments/feedback/suggestions.
Thanks :)

@Hashaam633
Copy link

thanks man
appreciate it man

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment