Thursday, September 11, 2008

c++ program to find the number of times that a given word(i.e. a short string) occurs in a sentence (i.e. a long string!).

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

void main()
{
char s1[20],s2[20];
int flag=0;

printf(" enter the first string");
gets(s1);
printf(" enter the second string");
gets(s2);
char *ptr1=s1;
char *ptr2=s2;
int count=0,flag1=0;
int m= strlen(s2);
while(1)
{
while(1)
{

char ch=*ptr1;
if(ch==' ')
break;
if(ch==NULL)
{
flag1=1;
break;
}
if(*ptr1==*ptr2)
{

*ptr1++;
*ptr2++;
count++;

}
else
{
count--;
*ptr1++;
}
}

if(flag1!=1)
{
if(*ptr1==' ')
ptr1++;
if(count==m)
{
flag++;
}
ptr2=s2;
count=0;
}
else
{
if(count==m)
{
flag++;
}
break;

}
}
if(flag!=0)
{
printf(" the no of word occurence is %d",flag);
}
else
{
printf(" word absent");
}
}

0 comments: