Untitled

mail@pastecode.io avatar
unknown
plain_text
2 months ago
1.0 kB
1
Indexable
Never
#include <stdio.h>
#include <string.h>

// DFA Transition function
int transition(int state, char input) {
    switch (state) {
        case 0:
            return (input == 'a') ? 1 : 0;
        case 1:
            return (input == 'b') ? 2 : 0;
        case 2:
            return 2;  // Accepting state
        default:
            return 0;  // Invalid state
    }
}

// DFA String acceptance check function
int isAccepted(char *input) {
    int currentState = 0;
    int len = strlen(input);

	int i;

    for (i = 0; i < len; i++) {
        currentState = transition(currentState, input[i]);
    }

    return (currentState == 2);  // Check if the final state is accepting (2)
}

int main() {
    char input[100];

    // Input from the user
    printf("Enter a string: ");
    scanf("%s", input);

    // Check if the input string is accepted
    if (isAccepted(input)) {
        printf("Accepted\n");
    } else {
        printf("Not Accepted\n");
    }

    return 0;
}
Leave a Comment