DFA Implementation C Program

 

✅ Language accepted: Strings ending with 01

Examples of accepted strings: 01, 101, 1101, 00001
Examples of rejected strings: 0, 1, 10, 111, 00


🔤 DFA States:

  • q0: Start state

  • q1: Saw 0

  • q2: Saw 01 (Accepting state)



✅ C Code:

#include <stdio.h>
#include <string.h> int main() { char input[100]; int state = 0; // Start state is q0 printf("Enter a binary string: "); scanf("%s", input); for (int i = 0; i < strlen(input); i++) { char ch = input[i]; switch (state) { case 0: // q0 if (ch == '0') state = 1; else if (ch == '1') state = 0; else { printf("Invalid input\n"); return 1; } break; case 1: // q1 if (ch == '0') state = 1; else if (ch == '1') state = 2; else { printf("Invalid input\n"); return 1; } break; case 2: // q2 if (ch == '0') state = 1; else if (ch == '1') state = 0; else { printf("Invalid input\n"); return 1; } break; } } if (state == 2) printf("String is accepted by the DFA.\n"); else printf("String is rejected by the DFA.\n"); return 0; }

🧠 Explanation:

  • The DFA moves between states based on the current character.

  • Only strings that end in the pattern 01 will leave the DFA in state q2.

  • state == 2 is the accepting condition.

Comments

Popular posts from this blog

Theory Of Computation PCCST302 KTU Semester 3 BTech 2024 Scheme

Non deterministic Finite Automata NFA

Example DFAs University Questions