Ticker

6/recent/ticker-posts

Dfa that accepts string on {0,1} if and only if the value of string interpreted as a binary representation of an integer is divisible by 2 C code.

 

Dfa that accepts string on {0,1} if and only if the value of string interpreted as a binary representation of an integer is divisible by 2 C code.

string accepted by language

={0,10,1000,1010,1110,.............................................}

#include<stdio.h>
#include<string.h>
int dfa=0;
char item;
void trasition0(char c)
{
if(c=='0')
{  
dfa=0;
}
else if(c=='1')
{
dfa=1;
}
else
dfa=-1;
}
void trasition1(char c)
{
if(c=='1')
dfa=1;
else if(c=='0')
    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