-
-
Save teeschorle/78f9b046b190436b42b8818dcbaf8a5e to your computer and use it in GitHub Desktop.
//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"); | |
} | |
} |
This was a big help in understanding the sequence of events, such as the else/if statements. I put several several lines in reverse order and it compiled but did not pass the check test. After reviewing your code I realized the mistakes. Thanks
I'm glad that I could help.
Hello guys - a question, did the cs 50 actually provided the wrong assumption in the course, which messes up actually my count of words. It was said quote "You may assume that a sentence will not start or end with a space, and you may assume that a sentence will not have multiple spaces in a row."
But when checking actual content in the automated tests it DOES HAVE sentences starting with space. Oh, not cool man, I think I do have wrong results because of that. I guess there needs to be a function which people pasted beforehand about clearing those nasty bastards.
Kindly any one can help me ! I do not know what is the problem here:
If I am not mistaken I think week 2 lecture talked about the need of declaring the size of an array.
I don't know if or how to declare the size in this problem.
I added wordcount++ at the end of my for loop coz at the end of the text is (".") so the code wont read it to add another word but a sentence
Why do you assign wordcount equal to 1? How to include sequence separated by one or more spaces to wordcount?
Because the last word will not be counted since it is not having space after it. So
I'm quite confused about Line 19. How would the computer be able to determine letters larger than 'a' or smaller than 'z', or will it automatically translate to ACSII? Thanks :)
@naaomiwang
Yes.
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
This was a big help in understanding the sequence of events, such as the else/if statements. I put several several lines in reverse order and it compiled but did not pass the check test. After reviewing your code I realized the mistakes. Thanks