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

Archives

program to find the number of substrings in a string

Tuesday, September 1, 2009

// number of times a substring occurs
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<string.h>
void main()
{  clrscr();
   char s1[500],s2[100];
   int i,j,c=0,l1,l2;
   cout<<"enter the string \n";
   gets(s1);
   cout<<"enter the substring\n";
   gets(s2);
   l1=strlen(s1);
   l2=strlen(s2);
   for(i=0;i<l1;i++)
   {   for(j=0;j<l2;j++)
       {   if(s1[i+j]!=s2[j])
       break;              }
    if(j==l2)
    c++;
   }
   cout<<"substring occurs in "<<c<<" times";
   getch();
   }

0 comments: