0

after struggling for days on end with scrabble, I decided to really spend my time consuming all the information possible from notes/lectures/shorts etc. Just finished readability only taking a few hours.

Is there any way I can improve my code here? are there any unnecessary or overcomplicated lines? are the comments too obvious? does it look well presented?

any feedback would be much appreciated, there's no real feedback from the cs50 course itself so its difficult to get an understanding on where and how you can improve code design/slickness etc. Oh and it works as intended and has been submitted already. thank you!

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

//calculate the reading level of a peice of text.

//declare functions
int count_letters(string c);
int count_words(string c);
int count_sentences(string c);
double grade_level(double a, double b, double c); 

int main(void)

{
// get text from the user. 

string text = get_string("Text: ");

// Assign variables to relevant functions.    

int letters = count_letters(text);
int words = count_words(text);
int sentences = count_sentences(text); 
double grade = grade_level(letters, words, sentences);

// Print reading grade level.
if (grade < 1)
{
    printf("Before Grade 1\n");
}
else if (grade > 16)
{
    printf("Grade 16+\n");  
}
else
{
    printf("Grade %.0f\n", round(grade));
}     
} 
// Calculate total letters in text.

int count_letters(string c)
{
int t = 0.0;
int i = 0;

for (int a = 0, n = strlen(c); a < n; a++)
{  
    if (isalpha(c[i]))
    {
        t++; 
    }
    i++;
}
return t;
}

// Calculate total words in text.

int count_words(string c)
{
int t = 1.0;
int i = 0;

for (int a = 0, n = strlen(c); a < n; a++)
{  
    if (isspace(c[i]))
    {  
        t++;
    }
    i++;
}
return t;
}  

//Calculate total sentences typed.

int count_sentences(string c)
{
int t = 0.0;
int i = 0.0;

for (int a = 0, n = strlen(c); a < n; a++)
{  
    if ((c[i] == '.') || (c[i] == '!') || (c[i] == '?'))
    {
        t++;
    }
    i++;
}
return t;
}

//Calculate reading Grade level.

double grade_level(double a, double b, double c) 
{ 

float L;
float S;
double y;  
//Calculate average letters per 100 words of text.
y = (100 / b);
L = (y * a);
// Calaculate average sentences per 100 words of text.

double z;
z = (100 / b);
S = (z * c);
// calculate reading level and return grade number.
double index;  
index = ((0.0588 * L) - (0.296 * S) - 15.8);
return index;
}

You must log in to answer this question.

Browse other questions tagged .