#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <regex.h>

int main(void) {
    regex_t reg;
    if (regcomp(&reg,"^(M{0,3})"
                    "(D?C{0,3}|CM|CD)"
                    "(L?X{0,3}|XC|XL)"
                    "(V?I{0,3}|IX|IV)$", REG_EXTENDED | REG_ICASE))
        exit(EXIT_FAILURE);

    char string[100];
    while (fgets(string, sizeof string, stdin)) {
        int len = strcspn(string, "\r\n");
        string[len] = '\0'; // chomp string
        if (len+1 != sizeof string) {
            int invalid = (len == 0  // empty input is invalid
                           || regexec(&reg, string, 0, NULL, 0));
            printf("'%s' is %svalid\n", string, invalid ? "in" : "");
        } else {
            fputs("input is too large\n", stderr);
            exit(EXIT_FAILURE);
        }
    }
    regfree(&reg);
    return !feof(stdin);
}
