#include "stdio.h"
#include "conio.h"
#include "string.h"
int state=0;
int count=0;
char *s,*pattern;
int Machine(int state,char input)
{
switch(state)
{
case 0: if (input==pattern[0]){state=2;}
else{state=1;}
break;
case 1:
if (input==pattern[0]){state=2;}
else{state=1;}
break;
case 2:
if (input==pattern[0]){state=2;}
if(input==pattern[1]){state=3;}
if(input!=pattern[0] && input!=pattern[1]){state=1;}
break;
case 3:
if (input==pattern[2]){state=4;count++;}
else{state=1;}
break;
case 4:
if (input==pattern[0]){state=2;}
else{state=1;}
break;
}
return state;
}
void main()
{
clrscr();
int arr[10];
char input;
printf("\nEnter the string: ");
scanf("%s",s);
printf("\nThe Given String is: %s",s);
printf("\nEnter the pattern: ");
scanf("%s",pattern);
for(int i=0;s[i]!='\0';i++)
{ input=s[i];
state=Machine(state,input);
}
printf("\ncat: occurs %d times",count);
getch();
}
#include "conio.h"
#include "string.h"
int state=0;
int count=0;
char *s,*pattern;
int Machine(int state,char input)
{
switch(state)
{
case 0: if (input==pattern[0]){state=2;}
else{state=1;}
break;
case 1:
if (input==pattern[0]){state=2;}
else{state=1;}
break;
case 2:
if (input==pattern[0]){state=2;}
if(input==pattern[1]){state=3;}
if(input!=pattern[0] && input!=pattern[1]){state=1;}
break;
case 3:
if (input==pattern[2]){state=4;count++;}
else{state=1;}
break;
case 4:
if (input==pattern[0]){state=2;}
else{state=1;}
break;
}
return state;
}
void main()
{
clrscr();
int arr[10];
char input;
printf("\nEnter the string: ");
scanf("%s",s);
printf("\nThe Given String is: %s",s);
printf("\nEnter the pattern: ");
scanf("%s",pattern);
for(int i=0;s[i]!='\0';i++)
{ input=s[i];
state=Machine(state,input);
}
printf("\ncat: occurs %d times",count);
getch();
}
No comments:
Post a Comment