Here is my code: the function that gives me errors is count_sentences
.
int count_letters(string text);
int count_words(string text);
int count_sentences(string text);
int main(void)
{
string text = get_string("Text: ");
printf("%s\n", text);
int letters = count_letters(text);
printf("%i letters\n", letters);
int words = count_words(text);
printf("%i words\n", words);
int sentences = count_sentences(text);
printf("%i sentences\n", sentences);
}
int count_letters(string text)
{
int letters = 0;
for (int i = 0, len = strlen(text); i < len; i++)
{
if (isalpha(text[i]))
{
letters++;
}
}
return letters;
}
int count_words(string text)
{
int words = 0;
for (int i = 0, len = strlen(text); i < len; i++)
{
if (text[i] == ' ')
{
words++;
}
}
return words + 1;
}
int count_sentences(string text)
{
int sentences = 0;
for (int i = 0, len = strlen(text); i < len; i++)
{
if (text[i] == '.' || '!' || '?')
{
sentences++;
}
}
return sentences;
}
Its giving me:
readability.c:66:28: error: use of logical '||' with constant operand [-Werror,-Wconstant-
logical-operand]
if (text[i] == '.' || '!' || '?')
^ ~~~
readability.c:66:28: note: use '|' for a bitwise operation
if (text[i] == '.' || '!' || '?')
^~
|
fatal error: too many errors emitted, stopping now [-ferror-limit=]
Anyone know how to fix it? I know I can just make an else if
block and get the same result but I would like to know if I could do it this way. it seems more effective and looks a lot nicer.
stackoverflow is giving me an error that is mostly code so ignore this please