0

I want to know how to count how many words are in a string. I use strstr to compare and it works but only works for one time

like this

char buff = "This is a real-life, or this is just fantasy";
char op = "is";

if (strstr(buff,op)){
    count++;
}
printf("%d",count);

and the output is 1 but there are two "is" in the sentence, please tell me.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
  • 4
    The initialization of your *single* character variables should make your compiler scream at you. As should the call to `strstr` which expects strings and not characters. – Some programmer dude Oct 15 '21 at 14:03
  • 1
    As for your problem, what does [`strstr`](https://en.cppreference.com/w/c/string/byte/strstr) *return*? And do you know how to use loops? – Some programmer dude Oct 15 '21 at 14:04
  • 2
    "but only works for one time" It's not in a loop so what did you expect... – Lundin Oct 15 '21 at 14:05
  • First of all you should use for or while loop to count the occurences. Secondly you must know proper use and return type for a function. strstr() is used when you want to find whether a substring exists in a given string or not, so this is inappropriate. Search on the internet as you will find a solution there. – Abhishek Agashe Oct 15 '21 at 14:12
  • "aaaaa" checked against "aa" should return what number? – Neil Oct 15 '21 at 16:40

2 Answers2

0

For starters you have to write the declarations at least like

char buff[] = "This is a real-life, or this is just fantasy";
const char *op = "is";

Also if you need to count words you have to check whether words are separated by white spaces.

You can do the task the following way

#include <string.h>
#include <stdio.h>
#include <ctype.h>

//...

size_t n = strlen( op );
for ( const char *p = buff; ( p = strstr( p, op ) ) != NULL; p += n )
{
    if ( p == buff || isblank( ( unsigned char )p[-1] ) )
    {
        if ( p[n] == '\0' || isblank( ( unsigned char )p[n] ) )
        {
            count++;
        }
    }
}
printf("%d",count);

Here is a demonstration program.

#include <stdio.h>
#include <string.h>
#include <ctype.h>

int main(void) 
{
    char buff[] = "This is a real-life, or this is just fantasy";
    const char *op = "is";
    size_t n = strlen( op );

    size_t count = 0;

    for ( const char *p = buff; ( p = strstr( p, op ) ) != NULL; p += n )
    {
        if ( p == buff || isblank( ( unsigned char )p[-1] ) )
        {
            if ( p[n] == '\0' || isblank( ( unsigned char )p[n] ) )
            {
                count++;
            }
        }
    }

    printf( "The word \"%s\" is encountered %zu time(s).\n", op, count );

    return 0;
}

The program output is

The word "is" is encountered 2 time(s).
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
0

Parse the string, in a loop.

As OP has "but there are two "is" in the sentence", it is not enough just to look for "is" as that occurs 4x, twice in "This". Code needs to parse the string for the idea of a "word".

Case sensitively is also a concern.

char buff = "This is a real-life, or this is just fantasy";
char op = "is";

char *p = buff;
char *candidate;
while ((candidate = strstr(p, op)) {
  // Add code to test if candidate is a stand-alone word
  // Test if candidate is beginning of buff or prior character is a white-space.
  // Test if candidate is end of buff or next character is a white-space/punctuation.
  p += strlen(op); // advance
}

For me, I would not use strstr(), but look for "words" with isalpha().

// Concept code
size_t n = strlen(op);
while (*p) {
  if (isalpha(*p)) {  // Start of word
    // some limited case insensitive compare
    if (strnicmp(p, op, n) == 0 && !isalpha(p[n]) {  
      count++;
    }
    while (isalpha(*p)) p++;  // Find end of word
  } else {
    p++;
  }
}
chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256