0
#include <stdio.h>
#include <string.h>
#include <cs50.h>
int main(void)
{
    string enterword = get_string("input: ");
    int t = strlen(enterword);
    printf("%i\n", t);
    int counter = 0;
    for(int i = 0; i<= t; i++)
    {
   
         if (enterword[i] == '.'|| enterword[i] == '!' || enterword[i] == '?'){
         counter = counter + 1;}
    }
         printf("number of lines %i\n",counter);
     
         int wordcounter = 0;
         for (int i = 0; i<= t; i++)
         {
             if (enterword[i] == ' ' || enterword[i] == '.'){
             wordcounter = wordcounter + 1;
         }
         
         }
         printf("total words %i\n", wordcounter);
 }

The above program fails to calculate correctly no. of words. The problem is that char type can take only one character.

int wordcounter = 0;
for (int i = 0; i<= t; i++)
     {
         if (enterword[i] == ' ' || enterword[i] == '.'){
         wordcounter = wordcounter + 1;
         }
      }
     printf("total words %i\n", wordcounter); 
     

I cannot have enterword[i] == '. '; that is, one space after. to count correctly number of words when before full stop and a new line starts with a space after that full stop. Seeking help how to approach in such scenario.

1 Answer 1

0

Here is a thread on CS50 eDX forum that led to the solution: https://edstem.org/us/courses/176/discussion/976408?comment=2210979

You must log in to answer this question.

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