cs50 Week 2 Problem Set 2 Readability
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.
The Solution:
#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);
}
}
0 Comments
Your opinion matters, your voice makes us proud and happy. Your words are our motivation.