# CS50 Readability Project (C Programming)

I’m currently taking CS50 and just “completed” (I think lol) the Readability assignment (i.e. assigning a grade level to a text based on the coleman-lindau formula).

Although the instructions recommended using individual functions to find the number of letters, words and sentences in a text, this would involve traversing the entire text 3 times and I wanted to traverse it only once.

I came up with the following solution (in C):

``````
// determine the average word length and average sentence length of the text

int letters = 0;
int words = 0;
int sentences = 0;
int i = 0;
while (i < strlen(text))
{
// check if the character is in the alphabet. If not, move on the the next character
while ((65 <= text[i]) && (text[i] <= 122))
{
// add the letter to the count
letters += 1;
// check what follows the letter
char let = text[i + 1];
// if the letter is followed by a space, increment words by 1 and skip the space
if (let == ' ')
{
words += 1;
i += 2;
continue;
}
// if the letter is followed by any of the below characters, increment words by 1 and skip the character and the space that follows
else if ((let == ',') | (let == ':') | (let == ';') | (let == '\"'))
{
words += 1;
i += 3;
continue;
}
// if the letter is followed by the below characters, increment words and sentences by 1 and skip both the character and the following space
else if ((let == '.') | (let == '!') | (let == '?'))
{
words += 1;
sentences += 1;
i += 3;
continue;
}
else
{
i++;
}
}
i++;
}
int letters_per_100_words = ((float) letters / words) * 100;
int sentences_per_100_words = ((float) sentences / words) * 100;

// use Coleman-Lindau equation on the results to determine the grade level

int coleman_lindau = round(0.0588 * letters_per_100_words - 0.296 * sentences_per_100_words - 15.8)```;
``````

I tested my code on all the texts provided by the CS50 instructors and got the same results for grade level on all except 1 (out of 10) of the texts provided by the instructors.

However, I also used an online word counter to check the results I got for letters, words, and sentences, and my code always has the exact same values as the word counter (including on the text for which I got a different result than the instructors).

Can anyone see where the problem is? Also I’m open in general to advice on how to improve the code overall (I’m very new to programming).

When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor (`</>`) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (').

It would help if you post your code in a way that makes it easier to read (see the above post).

Can you talk about your overall approach and what edge cases you think you may be not considering?