Write a program which will read a text and count all occurrences of a particular word.
#include<stdio.h>
#include <string.h>
#include<conio.h>
void main()
{
char a[100],b[20],c[20];
int i,count=0,k=0,len;
clrscr();
printf ("\nEnter a string:-> ");
gets(a);
printf("\nEnter the word to be searched:-> ");
gets(b);
len=strlen(a);
for(i=0;i<=len;i++)
{
//Assuming Space , Tab and NULL as word separator
if(a[i]==32 || a[i]=='\t' || a[i]=='\0')
{
c[k]='\0';
if(strcmp(b,c)==0)
count++;
k=0;
}
else
{
c[k]=a[i];
k++;
}
}
printf("Occurance of %s is = %d",b,count);
getch();
}
#include <string.h>
#include<conio.h>
void main()
{
char a[100],b[20],c[20];
int i,count=0,k=0,len;
clrscr();
printf ("\nEnter a string:-> ");
gets(a);
printf("\nEnter the word to be searched:-> ");
gets(b);
len=strlen(a);
for(i=0;i<=len;i++)
{
//Assuming Space , Tab and NULL as word separator
if(a[i]==32 || a[i]=='\t' || a[i]=='\0')
{
c[k]='\0';
if(strcmp(b,c)==0)
count++;
k=0;
}
else
{
c[k]=a[i];
k++;
}
}
printf("Occurance of %s is = %d",b,count);
getch();
}