Saturday 12 March, 2011

Write a program that will read a text and count all occurrences of a particular word

#include<stdio.h>
#include<conio.h>
main()
{
char str[100],find[100];
int i,j,cnt=0,flag;
clrscr();
printf("Enter the string :");
scanf("%s",str);
printf("Enter the srting that you want to find : ");
scanf("%s",find);
for(i=0;str[i];i++)
{
flag=0;
if(str[i]==find[0])
{
flag=1;
for(j=1;find[j];j++)
{
if(str[i+j]!=find[j])
{
flag=0;
break;
}
}
if(flag==1)
cnt++;
}
else
{
}
}
printf("%s occurs in %s %d times\n",find,str,cnt);
getch();
}
Enter the string :The cat & The dog
Enter the srting that you want to find : The
The occurs in The cat & The dog 2 times

No comments:

Post a Comment