Automata Lab Assignment
Assignment Lab Assignment
#include <string.h>
int match(char [], char []);
int 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");
}
return 0;
}
int match(char text[], char pattern[]) {
int c, d, e, text_length, pattern_length, position = -1;
text_length = strlen(text);
pattern_length = strlen(pattern);
if (pattern_length > text_length) {
return -1;
}
for (c = 0; c <= text_length - pattern_length; c++) {
position = e = c;
for (d = 0; d < pattern_length; d++) {
if (pattern[d] == text[e]) {
e++;
}
else {
break;
}
}
if (d == pattern_length) {
return position;
}
}
return -1;
}
Output:
Write a program to to simulate Nondeterministic Finite Automata (NFA).
Code:
#include<stdio.h>
#include<string.h>
int tranFun(char in, int state){
if(in=='0' && state==0){ return 0;}
else if(in=='1' && state==0){ return 2;}
else if(in=='0' && state==1){ return 3;}
else if(in=='1' && state==1){ return 0;}
else if(in=='0' && state==2){ return 1;}
else if(in=='1' && state==2){ return 3;}
else if(in=='0' && state==3){ return 4;}
else if(in=='1' && state==3){ return 1;}
else if(in=='0' && state==4){ return 2;}
else if(in=='1' && state==4){ return 4;}
else
return 0;
}
int main()
{
char in[100], temp[100];
printf("enter input");
scanf("%c",&in);
int state=0;
for(int i=0;i<strlen(in);i++){
state=tranFun(in[i],state);
printf("%d",state);
}
int len=strlen(in);
if(state==0)
printf("accepted");
else
printf("rejected");
}
Output:
Lab Assignment - 03
Write a C program to simulate deterministic finite automata (DFA).
Code: