Tuesday, 24 May 2016
C programs - Data structures c programs
Friday, 13 May 2016
BruteForce Pattern Matching Algorithm C program
Aim : To implement Pattern Matching Technique using Brute Force Algorithm
Program:
#include<stdio.h>
#include<string.h>
char t[100],p[50];
void main()
{
int pos;
clrscr();
printf("Enter the Source String ");
scanf("%s",t);
printf("Enter the pattern ");
scanf("%s",p);
pos=brute_force();
if(pos==-1)
printf("%s pattern not found in text",p);
else
printf("%s pattern found at index %d",p,pos);
getch();
}
int brute_force()
{
int n,j,m,i;
n=strlen(t);
m=strlen(p);
for(i=0;i<n;i++)
{
j=0;
while(j<m && t[i+j]==p[j])
{
j++;
if(j==m)
return i+1; //pattern found
}
}
return -1; //pattern not found
}
Output :
Thursday, 5 May 2016
Boyre Moore Pattern Matching Algorithm in C
Aim : To implement Pattern Matching Technique using Boyre Moore Algorithm
Program:
#include<string.h>
#include<stdio.h>
char t[20],p[20]; // t and p for to store text and pattern
int l[256]={-1}; //to store number occurence of each letter
void main()
{
int pos,i;
clrscr();
printf("Enter the Source String ");
scanf("%s",t);
printf("Enter the Pattern ");
scanf("%s",p);
for(i=0;p[i]!='\0';i++)
l[p[i]]=i;
pos=boyer_pattern();
if(pos==-1)
printf("%s pattern is found in the text ",p);
else
printf("%s pattern found at index %d",p,pos);
getch();
}
int boyer_pattern()
{
int n,m,i,j;
n=strlen(t);//length of text
m=strlen(p);//length of pattern
i=m-1;
j=m-1;
while(i<n)//until all characters in text are finished
{
if(t[i]==p[j])
{
if(j==0)
return i+1;
else
{
i--;
j--;
}
}
else
{
i=i+m-min(j,1+l[t[i]]);
j=m-1;
}
}
return -1;
}
int min(int x, int y)
{
if(x<y)
return x;
else
return y;
}
Output:
Subscribe to:
Posts (Atom)