[text] Sambhav-ans-1

Viewer

copydownloadembedprintName: Sambhav-ans-1
  1. #include <stdio.h>
  2. #include <string.h>
  3.  
  4. void main() {
  5.     int table[7][2], i, l, status = 0;
  6.     char input[100];
  7.     printf("To implement DFA of language (01)^*(21)^+(0/1)^*2\nEnter Input String: ");
  8.     // Transition table initialization
  9.     for (int j = 0; j < 7; j++) {
  10.         for (int k = 0; k < 2; k++) {
  11.             table[j][k] = -1; // Initialize to invalid state
  12.         }
  13.     }
  14.     // Transition definitions
  15.     table[0][0] = 1; // Transition from state 0 to state 1 on input '0'
  16.     table[1][1] = 2; // Transition from state 1 to state 2 on input '1'
  17.     table[2][0] = 3; // Transition from state 2 to state 3 on input '0'
  18.     table[3][1] = 4; // Transition from state 3 to state 4 on input '1'
  19.     table[4][0] = 5; // Transition from state 4 to state 5 on input '0'
  20.     table[4][1] = 6; // Transition from state 4 to state 6 on input '1'
  21.     table[5][0] = 5; // Transition from state 5 to state 5 on input '0'
  22.     table[5][1] = 6; // Transition from state 5 to state 6 on input '1'
  23.     table[6][0] = 5; // Transition from state 6 to state 5 on input '0'
  24.     table[6][1] = 6; // Transition from state 6 to state 6 on input '1'
  25.  
  26.     scanf("%s", input);
  27.     l = strlen(input);
  28.     for (i = 0; i < l; i++) {
  29.         if (input[i] != '0' && input[i] != '1') {
  30.             printf("Invalid input symbol: %c\n", input[i]);
  31.             return;
  32.         }
  33.         int symbolIndex = input[i] - '0'; // Convert char to int ('0' -> 0, '1' -> 1)
  34.         status = table[status][symbolIndex];
  35.         if (status == -1) {
  36.             printf("String not Accepted\n");
  37.             return;
  38.         }
  39.     }
  40.  
  41.     if (status == 2 || status == 3 || status == 5) {
  42.         printf("String Accepted\n");
  43.     } else {
  44.         printf("String not Accepted\n");
  45.     }
  46. }

Editor

You can edit this paste and save as new:


File Description
  • Sambhav-ans-1
  • Paste Code
  • 26 Apr-2024
  • 1.75 Kb
You can Share it: