WAP to check whether string is accepted or not for entered
grammar
#include<iostream.h>
#include<conio.h>
#include<string.h>
#include<stdio.h>
void main()
{
char string[20];
int state=0,count=0;
clrscr();
cout<<"the grammar is: S->aS, S->Sb, S->ab \n";
cout<<"enter the string to be checked \n";
gets(string);
while(string[count]!='\0')
{
switch(state)
{
case 0: if (string[count]=='a')
state=1;
else
state=3;
break;
case 1: if (string[count]=='a')
state=1;
else if(string[count]=='b')
state=2;
else
state=3;
break;
case 2: if (string[count]=='b')
state=2;
else
state=3;
break;
default: break;
}
count++;
if(state==3)
break;
}
if(state==2)
cout<<"string is accepted";
else
cout<<"string is not accepted";
getch();
}
#include<conio.h>
#include<string.h>
#include<stdio.h>
void main()
{
char string[20];
int state=0,count=0;
clrscr();
cout<<"the grammar is: S->aS, S->Sb, S->ab \n";
cout<<"enter the string to be checked \n";
gets(string);
while(string[count]!='\0')
{
switch(state)
{
case 0: if (string[count]=='a')
state=1;
else
state=3;
break;
case 1: if (string[count]=='a')
state=1;
else if(string[count]=='b')
state=2;
else
state=3;
break;
case 2: if (string[count]=='b')
state=2;
else
state=3;
break;
default: break;
}
count++;
if(state==3)
break;
}
if(state==2)
cout<<"string is accepted";
else
cout<<"string is not accepted";
getch();
}
OUTPUT:
1 comment:
Very good topic, similar texts are I do not know if they are as good as your work out. Excavator rental Mooresville
Post a Comment