0% found this document useful (0 votes)
26 views2 pages

Horspool Algorithm

The document contains a C program that implements the Horspool algorithm for pattern matching in a given text. It defines functions for creating a shift table and searching for a pattern within the text. The program prompts the user to input the text and pattern, then outputs the position of the pattern if found or indicates if it was not found.

Uploaded by

anisharaj0605
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views2 pages

Horspool Algorithm

The document contains a C program that implements the Horspool algorithm for pattern matching in a given text. It defines functions for creating a shift table and searching for a pattern within the text. The program prompts the user to input the text and pattern, then outputs the position of the pattern if found or indicates if it was not found.

Uploaded by

anisharaj0605
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

#include<stdio.

h>

#include<string.h>

#include<conio.h>

#define MAX 500

int t[MAX];

void shifttable(char p[])

int i,j,m;

m=strlen(p);

for(i=0;i<MAX;i++)

t[i]=m;

for(j=0;j<m-1;j++)

t[p[j]]=m-1-j;

int horspool(char src[],char p[])

int i,j,k,m,n;

n=strlen(src);

m=strlen(p);

printf("\nLength of text=%d",n);

printf("\n Length of pattern=%d",m);

i=m-1;

while(i<n)

k=0;

while((k<m)&&(p[m-1-k]==src[i-k]))

k++;

if(k==m)
return(i-m+1);

else

i+=t[src[i]];

return -1;

void main()

char src[100],p[100];

int pos;

clrscr();

printf("Enter the text in which pattern is to be searched:\n");

gets(src);

printf("Enter the pattern to be searched:\n");

gets(p);

shifttable(p);

pos=horspool(src,p);

if(pos>=0)

printf("\n The desired pattern was found starting from position %d",pos+1);

else

printf("\n The pattern was not found in the given text");

getch();

You might also like