C program to read a text and count all occurrences of a particular word


/* C program that will read a text and count all occurrences of a particular word. */

#include"stdio.h"

void main()
{
int i=0,j=0,count=0,f=0;
char str1[100],str2[20];
printf("Enter the text: ");
gets(str1);

printf("Enter word to count: ");
gets(str2);
for(i=0;str1[i];i++)
{
f=0;
if(str1[i]==str2[0])
f=1;
for(j=1;str2[j];j++)
{
if(str1[i+j]!=str2[j])
{
f=0;
break;
}
}
if(f==1)
count++;
else
{
}
}
printf("%s occur in %d times \n",str2,count);

}

Output:




Comments

Popular Posts