cs50 readability #include #include #include #include int count_letters(string text); int count_words(string text); int count_sentences(string text); int main(void) { cs50 readability How to get it for free? cs50 readability string text = get_string("Text: "); //printf("%i \n %i \n %i \n", count_letters(text), count_words(text), count_sentences(text)); float num_letters = (float) count_letters(text); float num_words = (float) count_words(text); float num_sentences = (float) count_sentences(text); //index = 0.0588 * L - 0.296 * S - 15.8 //L is the avg of letters per 100 words float L = (num_letters / num_words) * 100.0; //S is the avg of sentences per 100 words. cs50 readability How to use it? cs50 readability float S = (num_sentences / num_words) * 100.0; int index = (int) round(0.0588 * L - 0.296 * S - 15.8); //printf("%f %f %f\n", num_letters, num_words, num_sentences); //printf("%f %f %i\n", L, S, (int) round(I)); if (index > 1 && index < 16) { printf("Grade %i\n", index); } else if (index < 1) cs50 readability How to get it for free? cs50 readability { printf("Before Grade 1\n"); } else { printf("Grade 16+\n"); } } int count_letters(string text) cs50 readability PasteShr cs50 readability { int count = 0; for (int i = 0, length = strlen(text); i < length; i++) { int number = (int) text[i]; if ((number >= 97 && number <= 122) || (number >= 65 && number <= 90)) { count ++; } } cs50 readability How to use it? cs50 readability return count; } int count_words(string text) { int count = 0; for (int i = 0, length = strlen(text); i < length; i++) { //if at the last character end loop if (i == length - 1) cs50 readability How to use it? cs50 readability { //current char int number = (int) text[i]; int prev_number = (int) text[i - 1]; if ((number > 65 && number < 90) || (number > 97 && number < 122)) { count ++; } else if (prev_number != 32) cs50 readability How to dowload it? cs50 readability { count ++; } } else { //current char int number = (int) text[i]; //next char cs50 readability How to use it? cs50 readability int next_number = (int) text[i + 1]; //if the next char is a space if (next_number == 32) { //check if current char is not a space if (number != 32) { count ++; } cs50 readability How to get it? cs50 readability } } } return count; } int count_sentences(string text) { cs50 readability How to get it for free? cs50 readability int count = 0; for (int i = 0, length = strlen(text); i < length; i++) { int number = (int) text[i]; if (number == 33 || number == 63 || number == 46) { count++; } } return count; cs50 readability PasteShr cs50 readability } cs50 readability