String Pattern Matching in C using pointers


 

Block diagrams of (Left) pattern matching and ...
Block diagrams of (Left) pattern matching and substitution method and (Right) soft pattern matching method. (Photo credit: Wikipedia)

 

String Pattern Matching Program in C using Pointers

 

Its an application of Arrays

 

 

#include<stdio.h>
#include<conio.h>
int match(char*, char*);

main()
{
char a[100], b[100];
int position;

printf(“Enter some text\n”);
gets(a);

printf(“Enter a string to find\n”);
gets(b);

position = match(a, b);

if(position!=-1)
printf(“Found at location %d\n”, position+1);
else
printf(“Not found.\n”);

getch();
}

int match(char *a, char *b)
{
int c;
int position = 0;
char *x, *y;

x = a;
y = b;

while(*a)
{
while(*x==*y)
{
x++;
y++;
if(*x==”||*y==”)
break;
}
if(*y==”)
break;

a++;
position++;
x = a;
y = b;
}
if(*a)
return position;
else
return -1;
}

 

 

 

\n