0

I have written the program for readability to count the letters, words, and sentences and it does that. However, when printing the counted letters, words, sentences it does not just give me the total number of each but prints the whole sequence of the counting process. I know I am missing something simple. Please help!

include

include

include

include

include

int main(void) { //Takes input from users string s = get_string("Text: "); int letters = 0; int words = 1; int sentences = 0;

for (int i = 0; i < strlen(s); i++)

{
//Count letters
 if (isalpha(s[i]))
    {
     letters++;
    }

//Count words
 else if (isblank(s[i]))
    {
     words++;
    }

//Count sentences
 else if (s[i] == '.' || s[i] == '?' || s[i] == '!')
    {
     sentences++;
    }
 //prints sum of counts

printf("%i Letters, %i Words, %i Sentences \n", letters, words, sentences);

} }

1 Answer 1

1

The printf executes for every i because it is inside the for loop. If it should only execute when the for loop is complete, it needs to be outside the block (ie after the closing }).

1
  • oh wow. haha, how did I not realize that. Thank you so much!
    – user26024
    Commented Feb 27, 2020 at 18:15

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .