CS50 Readability Solution

By Suraj Chaudhary

Are you looking for the solution of Harvard’s CS50 PSet 2? Do you want the CS50 Readability Solution of week 2?

If so you’re in the right place.

In this article, I’m going to share with you exactly how to solve the CS50 Readability problem and will also share the CS50 Readability solution with you.

Here’s how to solve the CS50 Readability problem.

The first thing to do is to understand what we are trying to build. You can read the instructions from CS50’s website.

According to Scholastic, E.B. White’s Charlotte’s Web is between a second- and fourth-grade reading level, and Lois Lowry’s The Giver is between an eighth- and twelfth-grade reading level. What does it mean, though, for a book to be at a particular reading level?

Well, in many cases, a human expert might read a book and make a decision on the grade (i.e., year in school) for which they think the book is most appropriate. But an algorithm could likely figure that out too!

In a file called readability.c in a folder called readability, you’ll implement a program that calculates the approximate grade level needed to comprehend some text. Your program should print as output “Grade X” where “X” is the grade level computed, rounded to the nearest integer.

If the grade level is 16 or higher (equivalent to or greater than a senior undergraduate reading level), your program should output “Grade 16+” instead of giving the exact index number. If the grade level is less than 1, your program should output “Before Grade 1”.

Here’s my entire code for the CS50 Readability Solution:

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

int words(string s);
int letters(string s);
int sentences(string s);

int main(void)
{
    string text = get_string("Text: ");
    int W = words(text);
    int L = letters(text);
    int S = sentences(text);

    int index;
    index = round(0.0588 * (L / (float) W * 100.0) - 0.296 * (S / (float) W * 100.0) - 15.8);
    if (index < 1)
    {
        printf("Before Grade 1\n");
    }
    else if (index > 16)
    {
        printf("Grade 16+\n");
    }
    else
    {
        printf("Grade %i\n", index);
    }
}

int words(string s)
{
    int w = 0;
    int a = strlen(s);
    for (int i = 0; i < a; i++)
    {
        if (s[i] == ' ')
        {
            w++;
        }
    }
    return (w + 1);
}

int letters(string s)
{
    int l = 0;
    int a = strlen(s);
    for (int i = 0; i < a; i++)
    {
        if (isupper(s[i]) || islower(s[i]))
        {
            l++;
        }
    }
    return l;
}

int sentences(string s)
{
    int l = 0;
    int a = strlen(s);
    for (int i = 0; i < a; i++)
    {
        if (s[i] == '.' || s[i] == '!' || s[i] == '?')
        {
            l++;
        }
    }
    return l;
}

If you have any confusion or problem, feel free to drop a comment down below. In the meantime, you can read these articles.

Also Read

  1. CS50: How To Study At Harvard For FREE (and get a free certificate)
  2. How to use Windows 11 on Mac for Free
  3. How To Get A Domain Name For FREE
  4. How To Use Microsoft Office Apps For FREE Legally (Mac & PC)

Suraj Chaudhary is a writer, developer, founder, and a constant learner. He shares lessons and resources for living a fuller life every week. On this blog, he shares helpful guides and helpful articles that help his 70,000+ monthly readers find answers, solve problems, and meet their curious needs.

Leave a Comment

Slide to prove you're not a bot/spammer *