/**
 * Example for converting Arabian numerals to Roman numerals
 * Uses C99 dialect, so compile with -std=gnu99 or -std=c99
 * or via IDE-s Visual Studio 2013 (and higher) / DevC++5.7 (and higher)
 */
#include <stdio.h>  // printf, gets
#include <string.h> // strcpy, strcat, memset
#include <stdlib.h> // atoi


// Lookup table for converting arabian literals to roman
static const char* roman[3][10] = {
    {"","I","II","III","IV","V","VI","VII","VIII","IX"}, // ones
    {"","X","XX","XXX","XL","L","LX","LXX","LXXX","XC"}, // tens
    {"","C","CC","CCC","CD","D","DC","DCC","DCCC","CM"}  // hundreds
};


// Arabian to roman
// buffer - Used for input Arabian integer value and output Roman numeral string
char* ator(char* buffer, int bufferSize) 
{ 
    int i = atoi(buffer);
    int d = i / 1000; // how many thousands?

    // biggest roman numeral before thousands is 1888 (DCCCLXXXVIII) which is 12 chars
    // we also need a null terminator, so that makes the minimal buffersize 13 elements
    if (d > bufferSize - 13)
        return strcpy(buffer, "Buffer too small."); // no room to fill all the M's

    memset(buffer, 0, d + 1);  // null enough room
    memset(buffer, 'M', d);    // fill thousands (if any)
    strcat(buffer, roman[2][   (i = i % 1000)/100      ]); // hundreds
    strcat(buffer, roman[1][   (i = i % 100)/10        ]); // tens
    strcat(buffer, roman[0][   (i % 10) /*(i=i%10)/1*/ ]); // ones
    return buffer;
}


int main(int argc, char** argv) 
{
    char input[512];
    while (1) 
    {
        printf(">> Araabia number: ");
        if (*gets(input) == '\0') 
			break; // no input? - break
        printf(">> Rooma number:   %s\n", ator(input, sizeof input));
    }
    return 0;
}