Skip to content

Instantly share code, notes, and snippets.

@joshuaaguilar20
Created January 23, 2023 22:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joshuaaguilar20/bfe452322a2ac05a9277227cab4b3d0d to your computer and use it in GitHub Desktop.
Save joshuaaguilar20/bfe452322a2ac05a9277227cab4b3d0d to your computer and use it in GitHub Desktop.
Homework C Zach
#include <cs50.h>
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include <math.h>
int getNumLetters(string text);
int getNumSentences(string text);
int getNumWords(string text);
int getGradeLevel(int numLetters, int numSentences, int numWords);
void printGradeLevel(int gradeLevel);
int main(int argc, string argv[])
{
string text = get_string("Text: ");
int numLetters = getNumLetters(text);
int numSentences = getNumSentences(text);
int numWords = getNumWords(text);
int gradeLevel = getGradeLevel(numLetters, numSentences, numWords);
printGradeLevel(gradeLevel);
}
int getNumLetters(string text)
{
int numLetters = 0;
for (int i = 0; i < strlen(&text[i]); i++)
{
if (isalpha(text[i]))
{
numLetters += 1;
}
}
return numLetters;
}
int getNumSentences(string text){
int numSentences = 0;
for(int i = 0; i < strlen(&text[i]); i++){
if(text[i] == '.' text[i] == '?' text[i] == '!'){
numSentences += 1;
}
}
return numSentences;
}
int getNumWords(string text){
int numWords = 0;
for(int i = 0; i < strlen(&text[i]); i++){
if (text[i] == ' '){
numWords += 1;
}
}
return numWords + 1;
}
int getGradeLevel(int numLetters, int numSentences, int numWords){
float S = (numSentences / (float)numWords) * 100;
float L = (numLetters / (float)numWords) * 100;
return round(0.0588 * L - 0.296 * S - 15.8);
}
void printGradeLevel(int gradeLevel){
if (gradeLevel <= 1){
printf("Before grade 1\n");
}
else if(gradeLevel >= 16){
printf("Grade 16+\n");
}
else {
printf("%i\n", gradeLevel);
}
}
@joshuaaguilar20
Copy link
Author

joshuaaguilar20 commented Jan 23, 2023

The error in the C code block is in the getNumSentences() function. On this line:

Copy code
if(text[i] == '.' text[i] == '?' text[i] == '!'){

It should be:

Copy code

if(text[i] == '.' || text[i] == '?' || text[i] == '!'){

The current code is checking if the current character is equal to '.' and then checking if the current character is equal to '?' and then checking if the current character is equal to '!', which is not what you intended, it should be checking if the current character is equal to '.' OR the current character is equal to '?' OR the current character is equal to '!'.

You should also consider if you want to include other characters like ";",":","...", in your sentence count.

Please let me know if you have any other question.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment