Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 63 additions & 0 deletions readability.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
#include <stdio.h>
#include <cs50.h>
#include <ctype.h>
#include <string.h>
#include <math.h>


int main(void)
{
// Ask user for a line of text
string text = get_string("Text: ");

// Find the length of given string
int i = strlen(text);
int letters = 0;
int words = 0;
int sentences = 0;

// Count the letters using loop
for (int x = 0; x < i; x++)
{
// Counting letters
char c = text[x];
if (isalpha(c) != 0)
{
letters++;
}

// Now counts the words
if (c == ' ')
{
words++;
}

// Count the sentence
if (c == '.' || c == '!' || c == '?')
{
sentences++;
}
}

// To account for last word
words = words + 1;

// Calculation
float L = ((float)letters / (float)words) * 100;
float s = ((float)sentences / (float)words) * 100;
float subindex = 0.0588 * L - 0.296 * s - 15.8;
int index = round(subindex);
if (index > 16)
{
printf("Grade 16+\n");
}
else if (index < 1)
{
printf("Before Grade 1\n");
}
else
{
printf("Grade %i\n", index);
}

}