Untitled

mail@pastecode.io avatar
unknown
plain_text
2 months ago
1.4 kB
1
Indexable
Never
#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;
}
Leave a Comment