#include <stdio.h>

typedef enum {
    START,
    SIGN,
    DIGIT,
    INVALID
} State;

int is_valid_integer(const char* str) {
    State state = START;

    if (str == NULL || str[0] == '\0') {
        return 0;
    }

    for (int i = 0; str[i] != '\0'; i++) {
        char c = str[i];

        switch (state) {
            case START:
                if (c == '+' || c == '-') {
                    state = SIGN;
                } else if (c >= '0' && c <= '9') {
                    state = DIGIT;
                } else {
                    state = INVALID;
                }
                break;

            case SIGN:
                if (c >= '0' && c <= '9') {
                    state = DIGIT;
                } else {
                    state = INVALID;
                }
                break;

            case DIGIT:
                if (c >= '0' && c <= '9') {
                    state = DIGIT;
                } else {
                    state = INVALID;
                }
                break;

            case INVALID:
                return 0;
        }
    }

    return state == DIGIT;
}

int main() {
    const char* tests[] = {"123", "-456", "+78", "--12", "12a", "+", ""};
    int n = 7;

    for (int i = 0; i < n; i++) {
        printf("\"%s\" -> %s\n", tests[i],
               is_valid_integer(tests[i]) ? "VALID" : "INVALID");
    }

    return 0;
}
