Skip to main content

Pattern Matching in C

#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<stdlib.h>
void main()
	{
	int i,j,k=-1,s,r,max,comp=0;
	char t[50],p[50],c;
	clrscr();
	printf("ENTER TEXT = ");gets(t);s=strlen(t);
	PATTERN:
	printf("ENTER PATTERN = ");gets(p);r=strlen(p);
	max=s-r+1;
	//SEARCHING FOR PATTERN//
	for(i=0;i<=max;i++)
		{
		for(j=0;j<r;j++)
			{
			k=i;
			if(p[j]==t[i+j])
				{
				comp++;
				if(comp==r)
					{
					goto LOCK;
					}
				}
			else
				{
				k=-1;
				comp=0;
				break;
				}
			}
		}
	LOCK:
	if(k>-1)
		{
		printf("\nPATTERN IS AT LOCATION %d",k+1);
		}
	else
		{
		printf("\n\aSORRY PATTERN CANN'T FIND.");
		}
	printf("\nFIND PATTERN AGAIN ? (Y/N)..");
	c=getch();
	if(c=='y'||c=='Y')
		{
		k=-1;
		comp=0;
		goto PATTERN;
		}
	else
		exit(0);
	}


Spread the love

Mashkawat Ahsan

I am Mashkawat Ahsan. Wikipedian, Mozillian, tech activist, art lover. Thank you for visiting.

Leave a Reply

Your email address will not be published. Required fields are marked *