Untitled

 avatar
unknown
plain_text
a year ago
1.4 kB
3
Indexable
#include <stdio.h>
#include <stdbool.h>
#include <string.h>
#define MAX_LEN 100

// DFA transition function
int transition(int state, char input) {
    switch(state) {
        case 0:
            if (input == '0')
                return 1;
            else if (input == '1')
                return 0;
            break;
        case 1:
            if (input == '0')
                return 1;
            else if (input == '1')
                return 2;
            break;
        case 2:
            if (input == '0')
                return 1;
            else if (input == '1')
                return 0;
            break;
    }
    return -1; // Invalid transition
}

// DFA function to check if the input string ends with '01'
bool isEndingWith01(char *input) {
    int state = 0;
    int len = strlen(input);
    if (len < 2) // Input string should have at least 2 characters
        return false;

    // Traverse through the input string and transition DFA states
    for (int i = 0; i < len; i++) {
        state = transition(state, input[i]);
    }
    return (state == 2); // Accept if the final state is q2
}

int main() {
    char input[MAX_LEN];
    printf("Enter the string: ");
    scanf("%s", input);

    if (isEndingWith01(input)) {
        printf("Accepted\n");
    } else {
        printf("Rejected\n");
    }

    return 0;
}
Editor is loading...
Leave a Comment