Friday 19 May 2017

Write a Program to implement first Pattern Matching Algorithm to find the location of Given Pattern (P) in A Given Text (T).

Coding:

#include<iostream.h>
#include<string.h>
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
void main()
{
  clrscr();
  int k,max,index,t_len,p_len;
  char a[50],p[10];
  cout<<"\nEnter any text:";
  gets(a);
  cout<<"\nEnter pattern:";
  gets(p);
  t_len=strlen(a);
  p_len=strlen(p);
  k=0;
  max=t_len-p_len+1;
  while(k<max)
  {
   for(int i=0;i<p_len;i++)
   {
    if(p[i]!=a[k+i])
    {
      index=0;
      k++;
      break;
    }
    else
    {
      index=k;
      cout<<"\nPattern is exist In given text and position is:"<<index;
      getch();
      exit(0);
    }
   }
  }
  if(index==0)
  cout<<"\nPattern does not Exist:";
  getch();
 }


Output:

No comments:
Write comments