#include<stdio.h>
#include<conio.h>
void main()
{
char str1[50],str2[50];
int len1,len2,k,l,index,max,ch;
clrscr();
printf("\n Enter the String:-");
gets(str1);
printf("\n Enter the Pattern to be matched:-");
gets(str2);
len1=0;
while((ch=str1[len1])!='\0')
{ len1++; }
len2=0;
while((ch=str2[len2])!='\0')
{ len2++; }
k=1;
max=len1-len2+1;
while(k<=max)
{
for(l=0;l<len2;l++)
{
if (str2[l]!=str1[k+l-1])
{
goto abc;
}
}
index=k;
printf("\nIndex=%d",index);
getch();
exit(0);
abc: k++;
}
index=-1;
printf("\nPattern Not Matched");
getch();
}
Output:
#include<conio.h>
void main()
{
char str1[50],str2[50];
int len1,len2,k,l,index,max,ch;
clrscr();
printf("\n Enter the String:-");
gets(str1);
printf("\n Enter the Pattern to be matched:-");
gets(str2);
len1=0;
while((ch=str1[len1])!='\0')
{ len1++; }
len2=0;
while((ch=str2[len2])!='\0')
{ len2++; }
k=1;
max=len1-len2+1;
while(k<=max)
{
for(l=0;l<len2;l++)
{
if (str2[l]!=str1[k+l-1])
{
goto abc;
}
}
index=k;
printf("\nIndex=%d",index);
getch();
exit(0);
abc: k++;
}
index=-1;
printf("\nPattern Not Matched");
getch();
}
Output:
Post a Comment