Last active
October 22, 2022 18:32
-
-
Save teeschorle/78f9b046b190436b42b8818dcbaf8a5e to your computer and use it in GitHub Desktop.
CS50 Problem Set 2 (Fall 2019) - 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
//CS50 Problem Set 2 (Fall 2019): Readability | |
//Author: teeschorle | |
#include <stdio.h> | |
#include <cs50.h> | |
#include <string.h> | |
#include <math.h> | |
int main(void) | |
{ | |
string text = get_string("Text: "); | |
int letterscount = 0; | |
int wordcount = 1; | |
int sentencecount = 0; | |
//count words | |
for (int i = 0; i < strlen(text); i++) | |
{ | |
if ((text[i] >= 'a' && text[i] <= 'z') || (text[i] >= 'A' && text[i] <= 'Z')) | |
{ | |
letterscount++; | |
} | |
else if (text[i] == ' ') | |
{ | |
wordcount++; | |
} | |
else if (text[i] == '.' || text[i] == '!' || text[i] == '?') | |
{ | |
sentencecount++; | |
} | |
} | |
// printf("letters: %i; words: %i; sentences: %i\n", letterscount, wordcount, sentencecount); | |
float grade = 0.0588 * (100 * (float) letterscount / (float) wordcount) - 0.296 * (100 * (float) sentencecount / (float) wordcount) - 15.8; | |
if (grade < 16 && grade >= 0) | |
{ | |
printf("Grade %i\n", (int) round(grade)); | |
} | |
else if (grade >= 16) | |
{ | |
printf("Grade 16+\n"); | |
} | |
else | |
{ | |
printf("Before Grade 1\n"); | |
} | |
} |
hi guys.....why does int count_words have to be 1 and count_letter and count_words is 0????
Hey, so Word count is starting when the program sees a space using this:
else if (text[i] == ' ') { wordcount++; }
Remember, the word before the first space will not be counted so you have to start from 1.
#include <cs50.h>
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include <math.h>
int main(void)
{
int i;
string text;
do
{
text = get_string("Text: ");
}
while (text[0] == '\n' || text[0] == ' ' || text[0] == '\0' || ispunct(text[0]));
int len = strlen(text);
float letters = 0, words = 1, sentences = 0;
for (i = 0; i < len; i++)
{
// count letters
if (isalnum(text[i]))
{
letters++;
}
// count words
else if (isspace(text[i]) && text[i + 1] != '\0')
{
words++;
}
// count sentences
else if (text[i] == '.' || text[i] == '?' || text[i] == '!')
{
sentences++;
}
}
float index, L, S;
// evaluate average number of letters per 100 words
L = (letters / words) * 100;
// evaluate average number of sentences per 100 words
S = (sentences / words) * 100;
// evaluate the Coleman-Liau Index
index = 0.0588 * L - 0.296 * S - 15.8;
/* printf("Letters: %.2f\nWords: %.2f\nSentences: %.2f\nGrade %.0f\n", letters, words, sentences, round(index)); */
// for index value less than 1, print "Before Grade 1"
if (index < 1)
{
printf("Before Grade 1\n");
}
// for index value more than 16, print "Grade 16+"
else if (index > 16)
{
printf("Grade 16+\n");
}
// if neither of the above cases, print the index value obtained
else
{
printf("Grade %.0f\n", index);
}
}
Here's my code for this. Would appreciate any comments/feedback/suggestions.
Thanks :)
thanks man
appreciate it man
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@naaomiwang
Yes.