Ticker

6/recent/ticker-posts

C code of DFA that accept a string that have even number of 1.



C code of DFA that accept a string that have even number of 1. Like(00,11,1100,1010,1111,111111,etc).




#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=1;

}

else if(c=='1')

{

dfa=0;

}

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

return 0;

}

if(dfa==0)

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");

}

Post a Comment

0 Comments