CS50 PSet 2: Readability

A guide to the ‘Readability’ problem in CS50 Week 2.

Goal: To write a program that takes an input text and returns the grade level of complexity. This should be calculated using the Coleman-Liau Index, where L represents the average number of letters per 100 words in the text, and S is the average number of sentences per 100 words in the text.

index = 0.0588 * L - 0.296 * S - 15.8

The program should behave like the below example, with ‘Text:’ acting as a prompt for the user to enter the text to be assessed.

Where a text is rated below Grade 1, it should return ‘Before Grade 1’ and when it is rated above Grade 16 it should return ‘Grade 16+’.

Before beginning the main function, we must import the <math.h> and <string.h> libraries which we will use later, as well as the usual <stdio.h> and <cs50.h>.

The first step is then to prompt the user for the text, using get_string().

Now we can count the number of letters by looping through the length of the text using strlen() and incrementing l if the character is either an upper or lower case letter. The if condition looks for the ASCII codes for the letters. Note that l must be a float as we will be using it to perform calculations later on.

We use a similar loop to calculate the number of words, except we increment w every time we find a space. Note that we start w at 1 this time to account for the first word, which does not have a space before it.

Finally we count the number of sentences by searching for full stops, exclamation marks and question marks and incrementing s accordingly.

After we convert s and l into the averages per 100 words, the Coleman-Liau index can be calculated. The round() function from the math.h library is used here as the final result must be an integer.

With the index calculated, we can now print the grade level of the text.

This problem was useful for reinforcing the ASCII system and the importance of knowing when to use different data types, without getting too complex.