0% found this document useful (0 votes)
5 views1 page

Horspool String Matching Algorithm Implementation

The document presents a C program that implements the Horspool String Matching algorithm to search for a given pattern in a text string. It includes functions to read input strings, create a shift table, and perform the search. The program outputs the position of the found pattern or indicates if the pattern is not found.

Uploaded by

prasad-cs
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)
5 views1 page

Horspool String Matching Algorithm Implementation

The document presents a C program that implements the Horspool String Matching algorithm to search for a given pattern in a text string. It includes functions to read input strings, create a shift table, and perform the search. The program outputs the position of the found pattern or indicates if the pattern is not found.

Uploaded by

prasad-cs
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

Program 05: Develop the logic using Horspool String Matching algorithm

to implement a program to search for the given pattern in given text


string.
#include<stdio.h>
#include<string.h>

void main()
{
char T[100],P[100];
int flag;
clrscr();
printf("Enter the Text String \n");
gets(T);
printf("Enter the Pattren String \n");
gets(P);

flag = horspool(T,P,strlen(T),strlen(P));

if(flag == -1)
printf("String not Found \n");
else
printf("String found at posiion %d \n", flag);
}
//------------------------------------------------------------------
horspool(char T[100], char P[100], int n, int m)
{
char table[200];
int i,j;

for(i=0;i<n;i++)
table[T[i]] = m;

for(i=0;i<m-1;i++)
table[P[i]] = m-1-i;

i = m-1;

while( i < n )
{
j = 0;

while(j < m && T[i-j] == P[m-1-j])


j = j + 1;

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


else
i = i + table[T[i]];
}
return -1;
}

You might also like