Ticker

6/recent/ticker-posts

C code of DFA that accept a string that end with 1.

 

C code of DFA that accept a string that  end with 1.

#include<stdio.h>

#include<string.h>

int dfa=0;

void trasition0(char c)

{

if(c=='0')

{

dfa=1;

}

else if(c=='1')

{

dfa=2;

}

else

{

dfa=-1;

}

}

void trasition1(char c)

{

if(c=='0')

{

dfa=1;

}

else if(c=='1')

{

dfa=2;

}

else

{

dfa=-1;

}

}

void trasition2(char c)

{

if(c=='0')

{

dfa=1;

}

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 string010101

accepted

enter string0000

not accepted


Post a Comment

0 Comments