My photo
Bangalore, Karnataka, India
Extending one hand to help someone has more value rather than joining two hands for prayer

Archives

calculate the occurance of a word in the string

Tuesday, September 1, 2009

#include<iostream.h>
#include<stdio.h>
#include<conio.h>
void main()
{  clrscr();
   char s1[100],s2[100];
   cout<<"enter the string:"<<endl;
   gets(s1);
   cout<<"enter the word: ";
   gets(s2);

   int i,j,k,p;
   i=j=k=0;
   while(s2[i]!='\0')
   {  j++;i++;       }

   for(i=0;s1[i]!='\0';)
   {     if(s1[i]==s2[k])
      {    k++;
           if(k==j)
           p++;
           continue;   }
                     }
   cout<<"number of times the word occur is: "<<p;


   getch();
   }

0 comments: