0
int wordcount(char *str,int n)
  {
    int i=0,count=0,count1=0;

    for(i=0;i<strlen(str);i++)
      {
        if(str[i]!=' ' || str[i]!='\n' || str[i]!='\t')
         {
           count++;
         }

    else
    {
        if(count<=n)
        {
            count1++;
        }
    count=0;
    }


}
if(count<=n)
return (count1+1);
else
return count1;
}

count the number of words in str with number of characters equal to or less than length. The word must have whitespace on both sides (space, tab, newline or carriage return), unless it is at the beginning or end of the string str . For example if length == 3 , the function should be able to count all occurrences of words such as {the, in, a, of, all, ...etc}

My problem is: Whenever I enter the '\n' and '\t' and '\r' in the if statement with an or condition alongside ' ' it is giving 0 as answer but if I am only using ' ' it gives me correct answer.

Can anybody explain it to me?

Max
  • 11
  • 3

1 Answers1

1

Because you used i++ in the loop guard, the character you are testing inside the loop is the one after the one you just made sure wasn't the null terminating the string.

Scott Hunter
  • 48,888
  • 12
  • 60
  • 101
  • I got that but my main problem is still checking for tab and next line character and it is giving me wrong value as soon as I am adding these two characters with an or condition in the if loop. – Max Feb 23 '16 at 01:42