0% found this document useful (0 votes)
32 views3 pages

Program of Pattern Matching

This C program implements a pattern matching algorithm. It takes in a text string and pattern string as input. The match() function searches for an exact match of the pattern within the text. It returns the starting index position of the match within the text, or 0 if no match is found. The main() function gets the text and pattern from the user, calls match() to search for the pattern, and displays whether a match was found and the starting position.

Uploaded by

Ankur Gupta
Copyright
© Attribution Non-Commercial (BY-NC)
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)
32 views3 pages

Program of Pattern Matching

This C program implements a pattern matching algorithm. It takes in a text string and pattern string as input. The match() function searches for an exact match of the pattern within the text. It returns the starting index position of the match within the text, or 0 if no match is found. The main() function gets the text and pattern from the user, calls match() to search for the pattern, and displays whether a match was found and the starting position.

Uploaded by

Ankur Gupta
Copyright
© Attribution Non-Commercial (BY-NC)
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
You are on page 1/ 3

(10)WAP in C to implementPattern Matching.

#include<iostream.h>

#include<string.h>

#include<conio.h>

#include<stdio.h>

int match(char text[],char pattern [])

int match,i,j,k=0;

if(strlen(pattern)>strlen(text))

return 0;

for(i=0;i<strlen(text);i++)

k=0;

for(j=i;k<strlen(pattern);j++,k++)

if(pattern[k]!=text[j])

match=0;

break;

else

match=1;

}
if(match==1)

if((i==0 && text[j]==' ')||(text[i-1]==' '&& text[j]==' ')||


(text[i-1]==' '&& text[j]==NULL))

return (i+1);

return 0;

main()

char text[25],pattern[25];

int posi;

clrscr();

cout<<"Enter the texct:-";

gets(text);

cout<<"Enter the pattern for matching";

gets(pattern);

posi=match(text,pattern);

if(posi==0)

cout<<pattern<<" does not exit in"<<text;

else

cout<<pattern<<"start at "<<posi;
getch();

You might also like