Tuesday, 18 March 2014

Program In C++ To Implement Naive String Matching Algorithm

#include<iostream.h>
#include<conio.h>
void naive(int t[],int p[],int n,int m)
{
 int s,flag=0,i,j;
 for(s=0;s<=(n-m);s++)
{
 for(i=s+1;i<=s+m;i++)
 {
  for(j=1;j<=m;j++)
  {
   if(t[s+j]==p[j])
    {
     flag=1;
     continue ;
    }
   else
   {
    flag=0;
    break;
   }
  }
 }
 if(flag==1)
  {
   cout<<"\nPattern Occurs With Shift: "<<s<<"\n";
  }
 }
}

void main()
{ clrscr();
int p[100],t[20];
int m,n,i;
cout<<"Enter Length Of Text : ";
cin>>n;
cout<<"\nEnter Text : ";
for(i=1;i<=n;i++)
{ cin>>t[i]; }
cout<<"\nEnter Length Of Pattern: ";
cin>>m;
cout<<"\nEnter Pattern: ";
for(i=1;i<=m;i++)
{ cin>>p[i]; }
naive(t,p,n,m);
getch();
}

No comments:

Post a Comment