Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 4

Lab-3:

Write a program to implement DFA over {0,1} that accept all the string that
ends with 101.

Deterministic Finite Automata:


A deterministic finite automaton is defined by a quintuple (5-tuple) as (Q, Σ, δ,
q0, F). Where,
Q = Finite set of states,
Σ = Finite set of input symbols,
δ = A transition function that maps Q × Σ → Q q0 = A start state; q0 ∈Q
F = Set of final states; F Q.
A transition function δ that takes as arguments a state and an input symbol
and returns a state.

Source code:
#include <stdio.h>
enum states
{
q0,
q1,
q2,
qf
};
enum states delta(enum states, char);
int main()
{
char input[20];
enum states curr_state = q0;
int i = 0;
printf("\n Enter a binary string:\n");
gets(input);
char ch = input[i];
while (ch != '\0')
{
curr_state = delta(curr_state, ch);
ch = input[++i];
}
if (curr_state == qf)
printf("\n The string %s is accepted.", input);
else
printf("\n The string %s is not accepted.", input);
return 0;
}
enum states delta(enum states s, char ch)
{
enum states curr_state;
switch (s)
{
case q0:
if (ch == '1')
curr_state = q1;
else
curr_state = q0;
break;
case q1:
if (ch == '0')
curr_state = q2;
else
curr_state = q1;
break;
case q2:
if (ch == '1')
curr_state = qf;
else
curr_state = q0;
break;
case qf:
if (ch == '0')
curr_state = q2;
else
curr_state = q1;
break;
}
return curr_state;
}
Output:

You might also like