Last active
April 15, 2022 07:28
-
-
Save dasunsucharith/2da9553cf9d7b564e4d38ee44b5d91fb to your computer and use it in GitHub Desktop.
My solution to CS50 2022 psets-2 readability
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <cs50.h> | |
#include <stdio.h> | |
#include <ctype.h> | |
#include <string.h> | |
#include <math.h> | |
int count_letters (string text); | |
int count_words (string text); | |
int count_sentences(string text); | |
int main(void) | |
{ | |
// Getting User Inputs | |
string text = get_string("Text: "); | |
// Output the number of letters in the phrase | |
int letters = count_letters(text); | |
// Output the number of words in the phrase | |
int words = count_words(text); | |
// Outout the number of sentence in the phrase | |
int sentences = count_sentences(text); | |
printf("%i letters\n", letters); | |
printf("%i words\n", words); | |
printf("%i sentences\n", sentences); | |
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); | |
printf("Grade %.2f\n", L); | |
printf("Grade %.2f\n", S); | |
if (index < 1) | |
{ | |
printf("Before Grade 1\n"); | |
} | |
else if (index >= 16) | |
{ | |
printf("Grade 16+\n"); | |
} | |
else | |
{ | |
printf("Grade %i\n", index); | |
} | |
} | |
// Count the number of letters in the phrase | |
int count_letters (string text) | |
{ | |
int letter_count = 0; | |
for (int i = 0; i < strlen(text); i++) | |
{ | |
/*if (text[i] >= 65 && text[i] <= 90) | |
{ | |
letter_count++; | |
} | |
else if (text[i] >= 97 && text[i] <= 122) | |
{ | |
letter_count++; | |
}*/ | |
if (isalpha(text[i])) | |
{ | |
letter_count++; | |
} | |
} | |
return letter_count; | |
} | |
// Count the number of words in the phrase | |
int count_words (string text) | |
{ | |
int word_count = 0; | |
for (int i = 0; i < strlen(text); i++) | |
{ | |
if (isspace(text[i])) | |
{ | |
word_count++; | |
} | |
} | |
return word_count + 1; | |
} | |
// Count the number of sentences in the phrase | |
int count_sentences(string text) | |
{ | |
int sentence_count = 0; | |
for (int i = 0; i < strlen(text); i++) | |
{ | |
if (text[i] == 46 || text[i] == 33 || text[i] == 63) | |
{ | |
sentence_count++; | |
} | |
} | |
return sentence_count; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment