Skip to content

Instantly share code, notes, and snippets.

@alrafiabdullah
Created May 29, 2021 21:04
Show Gist options
  • Save alrafiabdullah/7e337c0398d0a35c58757b391de795ab to your computer and use it in GitHub Desktop.
Save alrafiabdullah/7e337c0398d0a35c58757b391de795ab to your computer and use it in GitHub Desktop.
Readability checker written with C, according to Scholastic standards.
#include <stdio.h>
#include <cs50.h>
#include <string.h>
#include <ctype.h>
#include <math.h>
int letterCounter(char paragraph[], int);
int wordCounter(char paragraph[], int);
int sentenceCounter(char paragraph[], int);
int main(void) {
int totalLetters, totalWords, totalSentences, length;
float index, letter100, sentence100;
string paragraph;
paragraph = get_string("Enter your text: ");
length = strlen(paragraph);
char letterArray[strlen(paragraph)];
for(int i=0; i<strlen(paragraph); i++) {
letterArray[i]=paragraph[i];
}
totalLetters = letterCounter(letterArray, length);
totalWords = wordCounter(letterArray, length);
totalSentences = sentenceCounter(letterArray, length);
letter100 = ((totalLetters*100)/totalWords);
sentence100 = ((totalSentences*100)/totalWords);
index = (0.0588*letter100) - (0.296*sentence100) - 15.8;
// printf("%f\n", index);
if (index > (floor(index)+.54f)) {
index = ceil(index);
} else {
index = floor(index);
}
// printf("%f\n", index);
printf("Text: %s\n", paragraph);
printf("%d letter(s)\n", totalLetters);
printf("%d word(s)\n", totalWords);
printf("%d sentence(s)\n", totalSentences);
if(index < 1) {
printf("Before Grade 1\n");
return 0;
} else if (index >= 16) {
printf("Grade 16+\n");
return 0;
} else {
printf("Grade %.0f\n", index);
return 0;
}
}
int letterCounter(char paragraph[], int length) {
int count = 0;
for(int i=0; i<length; i++) {
paragraph[i] = tolower(paragraph[i]);
if (paragraph[i] > 96 && paragraph[i] < 123) {
count += 1;
}
}
return count;
}
int wordCounter(char paragraph[], int length) {
int count = 0;
for(int i=0; i<length; i++) {
paragraph[i] = tolower(paragraph[i]);
if (paragraph[i] == 32) {
count += 1;
}
}
return count+1;
}
int sentenceCounter(char paragraph[], int length) {
int count = 0;
for(int i=0; i<length; i++) {
paragraph[i] = tolower(paragraph[i]);
if (paragraph[i] == 46 || paragraph[i] == 33 || paragraph[i] == 63) {
count += 1;
}
}
return count;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment