I'm fairly new to coding. I'm having trouble with my "CountSentences" function. I compare the string to "." , "?" , and ! to count a sentence. It only adds one to the sentence counter no matter how many of the punctuation marks I have in the string. Am I using strcmp wrong to get my desired result and is there another way I can approach this?
#include<cs50.h>
#include<ctype.h>
#include<string.h>
#include<math.h>
//function for letter count
int count_letters(string s)
{
int numberofLetters = 0; // counter
//loop as long as string length
for(int i = 0, n = strlen(s); i < n; i++)
{
//if character is alphanumeric
if(isalnum(s[i]) != 0)
{
numberofLetters++; //increase counter
};
};
return numberofLetters; //return new counter number
};
//function for word count
int count_Words(string w)
{
int numberofWords = 0;//counter for words declared
int i = 0; // counter for character in string
if(w == NULL) // if nothing
{
return numberofWords; // return Wordcount of 0
};
bool spaces = true; //truth value for space
//if character is not null terminating character
while(w[i] != '\0')
{
if(isblank(w[i]) != 0) //if character is blank
{
spaces = true; //its a space
}
else if(spaces) //if no more space and a letter is present add to words
{
numberofWords++; //add to number of words counter
spaces = false;
};
i++;// increase chracter count in string w
};
return numberofWords; //return total word counter
};
//function to count sentences
int count_Sentences(string l)
{
//variable counter for marks
int countMarks = 0;
//loop iteration using the number of characters in string
for(int i = 0, n = strlen(l); i < n; i++)
{
//check if character is ?, . , or !
if(strcmp(&l[i], "!") == 0 || strcmp(&l[i], ".") == 0 || strcmp(l, "?") == 0)
{
countMarks++;// sentence counted
};
};
// return the total number of marks
return countMarks;
};
int main (void)
{
string text = get_string ("Text: ");
//to check the functions bug checker
printf("Number of letters: %i\n", count_letters(text));
printf("Number of words: %i\n", count_Words(text));
printf("Number of sentences: %i\n", count_Sentences(text));
//Coleman Liau Index
int grade = round(0.0588 * (100 * (count_letters(text)) / (count_Words(text))) - 0.296 * (100 *(count_Sentences(text)) / (count_Words(text))) - 15.8 );
if(grade <= 1)
{
printf("Before Grade 1\n");
}
else if(grade < 16)
{
printf("Grade %i\n", grade);
}
else
{
printf("Grade 16+\n");
};
};
if(l[i] == '!')etc. Aside: it's ill-advised to usel(the letter ELL) as variable name. It is too easily confused with1(the digit ONE) making the code hard to read, and open to mistakes.ispunct()andisspace()andisalpha()and so on, declared inctype.h.;after a closing brace}#include <stdio.h>for theprintf()and similar functionsif(strcmp(&l[i], "!") == 0 || strcmp(&l[i], ".") == 0 || strcmp(l, "?") == 0)This will NOT work because a single char froml[]is not a string. Suggest:if( l[i] == '!' || l[i] == '.' || i[i] == '?' )Notice the use of single quotes rather than double quotes so comparing a character rather than a string. Notice the use of appropriate horizontal spacing for readability.