I want to make a code in c for searching how many times a string is found inside a txt file using strstr() function.
I have made a test code with strastr() but I have a problem.
e.g I have sentence like " this is a text"
and when I search for "is"
I get a result "is found 2 times"
because it takes the "is" from "this". I don't want to take the is
from this
i want "is" as only word. Can i search without this "problem" with strstr() with some changes
#include <stdio.h>
#include<string.h>
int main()
{
char*ptr;
char input[]=("this is a text");
char key[10];
int counter;
scanf("%s",key);
ptr=strstr(input,key);
while (ptr==NULL)
{
printf("not found\n");
break;
}
while(ptr!=NULL)
{
counter++;
ptr=strstr(ptr+1,key);
}
printf("%s found %d times\n",key,counter);
return 0;
}