Start Your Journey with Linux Command Line
![]() |
| Problem Set 2 - Readability |
In coding, problems have different ways to get to the solution. Below code is one way. Soon, we will be posting different codes.
** Remember:
Try to change some things in the code before submitting it in order to avoid being rejected as Harvard academic honesty.
#include <cs50.h>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <ctype.h>
int main(void)
{
// inintiate needed variables
string text = get_string("TEXT: ");
int letters = 0;
int words = 1;
int sentences = 0;
for (int i = 0; i < strlen(text); i++)
{
if (isalpha(text[i]))
{
letters++;
}
else if (text[i] == ' ')
{
words++;
}
else if (text[i] == '.' || text[i] == '?' || text[i] == '!')
{
sentences++;
}
}
// Initiate variables needed to construct the formula
float L = (float) letters / (float) words * 100;
float S = (float) sentences / (float) words * 100;
int index = round(0.0588 * L - 0.296 * S - 15.8);
if (index < 1)
{
printf("Before Grade 1\n");
}
else if (index > 16)
{
printf("Grade 16+\n");
}
else
{
printf("Grade %i\n" , index);
}
}
The CS50 Readability problem asks you to implement the Coleman-Liau readability index, which estimates the U.S. grade level needed to understand a given text. The formula uses two measurable quantities: the average number of letters per word and the average number of sentences per word. Specifically, the index is calculated as: L (average letters per 100 words) multiplied by 0.0588, minus S (average sentences per 100 words) multiplied by 0.296, minus 15.8. The result is rounded to the nearest whole number to determine the grade level.
To implement this, you first need to count three things from the input text: the total number of letters (alphabetic characters), the total number of words (sequences of characters separated by spaces), and the total number of sentences (characters ending with ., !, or ?). Once you have these counts, you calculate L and S by dividing each count by the total number of words and then multiplying by 100.
The solution begins by reading a line of text from the user using get_string. You then loop through each character in the string, using isalpha to count letters, checking for spaces to count words, and checking for sentence-ending punctuation to count sentences. A common approach is to initialize a word counter that increments whenever a space is found, being careful to handle the edge case where the last word in the text is not followed by a space. After the loop, the final word count is incremented by one to account for that last word.
L) and sentences per 100 words (S).isalpha(), which excludes spaces, digits, and punctuation.Only alphabetic characters count as letters. Spaces, digits, punctuation marks, and special characters are not counted. The isalpha() function in C is the standard way to identify letters in each character of the input text.
The standard approach is to increment a word counter each time you encounter a space character. However, you must add one to the final count after the loop ends, because the last word in the text is not followed by a space. An alternative approach is to count transitions from non-space to space characters throughout the string.
If the calculated grade level rounds to 1 or below, the program should print "Before Grade 1." This indicates that the text is easy enough for a very young reader to understand.
Multiplying by 100 normalizes the counts to a "per 100 words" basis. This standardization ensures the formula produces consistent grade level estimates regardless of the total length of the text being analyzed.
Use floating-point arithmetic for the intermediate calculations (L, S, and the final formula result) to maintain precision. Only convert to an integer when rounding the final result to determine the grade level. Using integer division too early will introduce rounding errors that produce incorrect outputs.
Comments
Post a Comment
Your opinion matters, your voice makes us proud and happy. Your words are our motivation.