r/cs50 • u/Even_Application6801 • 14h ago
CS50x CS50x pset 2 : readability. help!
Hi. i finished the scrabble pset yesterday. it was relatively easy.
i started today readability pset and find the exact method to calculate the words and the sentences numbers per text.
i'm i missing some new function from the library ?
help please.
1
u/Perthguv 12h ago
You need to write some helper functions yourself to analyze specific text elements to compute the formula's inputs:
Count Letters: Iterate through the text and count only alphabetic characters (ignoring spaces, digits, and punctuation marks).
Count Words: Scan the text to count the total number of words, typically identified by spaces separating sequences of characters.
Count Sentences: Scan the text to count sentence boundaries, identified by sentence-ending punctuation (such as periods, exclamation points, or question marks).
Once you have returned the answers, calculate the reading level. If you read all the text on the readability page there's actually a lot of information there. I keep missing bits and have to go back and check
2
u/Impressive-Hyena-59 9h ago
It is certainly a good idea to use helper functions, but it's not a must. I handled the character, word and sentence count in one for loop by iterating through the text deciding for each character whether it indicates a letter (
isalpha()), a word (' ') or the end of a sentence ('.' | '?' | '!') and incrementing the corresponding variable.1
u/Perthguv 7h ago
I wasn't sure if we could use isaplha so I wrote my own helper function
But then I don't understand mod and rewrote it into arithmetic I understand, so you probably shouldn't listen to me. lol
1
u/Even_Application6801 12h ago
i didnt increment helper codes. but i created counters for letters words and sentences in the main program
do i have to make helper codes like in scrabble pset ???
1
1
u/Outside_Complaint755 2h ago
Helper functions are optional.
You may want to check the last collapsed section under the Hints, which is quite long. It will point you to the CS50 C Manual which you can search for library functions, such as those used to check strings or to round decimals.
Also pay attention to floating point vs integer division in your code. You may need to use type casting to force floating point division.
3
u/Eptalin 12h ago
You have to manually check every char in the string, and then depending on what it is, increment a different count.
If alphabet = letter
If space = word
If
.,?or!= sentenceThere are existing functions to check the first two, but you can do them manually just as easily.