Ticker

6/recent/ticker-posts

C code of DFA that accept a string that have consecutive 11 as substring.


Deterministic finite automata(DFA) simulation in C code

C code of DFA that accept a string that have  consecutive 11 as substring. 

#include<stdio.h>

#include<string.h>

int dfa=0;

void trasition0(char c)

{

if(c=='0')

{

dfa=0;

}

else if(c=='1')

{

dfa=1;

}

else

{

dfa=-1;

}

}

void trasition1(char c)

{

if(c=='0')

{

dfa=0;

}

else if(c=='1')

{

dfa=2;

}

else

{

dfa=-1;

}

}

void trasition2(char c)

{

if(c=='0')

{

dfa=2;

}

else if(c=='1')

{

dfa=2;

}

else

{

dfa=-1;

}

}

int isaccepted(char str[])

{

int i;

int len=strlen(str);

for(i=0;i<len;i++)

{

if(dfa==0)

trasition0(str[i]);

else if(dfa==1)

trasition1(str[i]);

else if(dfa==2)

trasition2(str[i]);

else

return 0;

}

if(dfa==2)

return 1;

else 

return 0;

}

int main()

{

char str[100];

printf("enter string");

scanf("%s",str);

if(isaccepted(str))

{

printf("accepted");

}

else

{

printf("not accepted");

}

}

Output
enter string101010100
not accepted
enter string0110
accepted

Post a Comment

0 Comments