0% found this document useful (0 votes)
41 views3 pages

Write A C Program To Simulate A DFA.: Aim: Algorithm

This C program simulates a deterministic finite automaton (DFA). It defines a node structure to represent the DFA states, with fields for the state ID, status, and links to next states on 0 or 1 input. It takes user input to define the number of states, state transitions, initial state, and final states. It then accepts a binary string as input and traces the states visited to determine if the string is accepted by the DFA.

Uploaded by

aditi bhatt
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
41 views3 pages

Write A C Program To Simulate A DFA.: Aim: Algorithm

This C program simulates a deterministic finite automaton (DFA). It defines a node structure to represent the DFA states, with fields for the state ID, status, and links to next states on 0 or 1 input. It takes user input to define the number of states, state transitions, initial state, and final states. It then accepts a binary string as input and traces the states visited to determine if the string is accepted by the DFA.

Uploaded by

aditi bhatt
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

AIM: Write a C program to simulate a DFA.

ALGORITHM:

PROGRAM:
#include <stdio.h>
#include <stdlib.h>
struct node{
int id_num;
int st_val;
struct node *link0;
struct node *link1;};
struct node *start, *q, *ptr;
int vst_arr[100], a[10];
int main(){
int count, i, posi, j;
char n[10];
printf("=-=-=-=-=-=-=-=-=-=-=-=\n");
printf("Enter the number of states in the m/c:");
scanf("%d",&count);
q=(struct node *)malloc(sizeof(struct node)*count);
for(i=0;i<count;i++){
(q+i)->id_num=i;
printf("State Machine::%d\n",i);
printf("Next State if i/p is 0:");
scanf("%d",&posi);
(q+i)->link0=(q+posi);
printf("Next State if i/p is 1:");
scanf("%d",&posi);
(q+i)->link1=(q+posi);
printf("Is the state final state(0/1)?");
scanf("%d",&(q+i)->st_val); }
printf("Enter the Initial State of the m/c:");
scanf("%d",&posi);
start=q+posi;
printf("=-=-=-=-=-=-=-=-==-=-=-=-=-=-=-=-=-=-=-=\n");
while(1){
printf("=-=-=-=-=-=-=-=-==-=-=-=-=-=-=-=-=-=-=-=\n");
printf("Perform String Check(0/1):");
scanf("%d",&j);
if(j){
ptr=start;
printf("Enter the string of inputs:");
scanf("%s",n);
posi=0;
while(n[posi]!='\0'){
a[posi]=(n[posi]-'0');
posi++; }
i=0;
printf("The visited States of the m/c are:");
do{
vst_arr[i]=ptr->id_num;
if(a[i]==0){
ptr=ptr->link0;}
else if(a[i]==1){
ptr=ptr->link1;}
else{
printf("iNCORRECT iNPUT\n");
return;}
printf("[%d]",vst_arr[i]);
i++;
}while(i<posi);
printf("\n");
printf("Present State:%d\n",ptr->id_num);
printf("String Status:: ");
if(ptr->st_val==1)
printf("String Accepted\n");
else
printf("String Not Accepted\n");}
else
return 0;}
printf("=-=-=-=-=-=-=-=-==-=-=-=-=-=-=-=-=-=-=-=\n");
return 0;}
OUTPUT:

RESULT:

You might also like