#include <iostream>
#include <cstring>

using namespace std;

const char *SYM[] = {"M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "V", "IV", "I"};
const int  NUM[] = {1000, 900, 500, 400, 100, 90, 50, 40, 10, 5, 4, 1};

int romanToIndian(char *s);
void indianToRoman(int n);

int
main(void) {
 char roman[100];
 int indian;

 cout << "Enter a roman number: ";
 cin >> roman;

 cout << "Value: " << romanToIndian(roman) << endl;

 cout << "Enter an indan number: ";
 cin >> indian;
 cout << "Roman Value: ";
 indianToRoman(indian);
 cout << endl;

 return 0;
}


int
romanToIndian(char *s) {
 int symLen;
 int result = 0;

 //loop through all the characters
 while(*s) {
   for(int i=0; i<sizeof(SYM) / sizeof(SYM[0]); i++) {
     symLen = strlen(SYM[i]);

     if(!strncmp(s, SYM[i], symLen)) {
       //i've found the symbol
       result += NUM[i];

       //update
       s += symLen;
       break;
     }
   }
 }

 return result;
}

void
indianToRoman(int n) {
 //go through the values
 for(int i=0; i<sizeof(NUM)/sizeof(NUM[0]); i++) {
   //display the correct number of that roman numeral
   for(int j=0; j<n/NUM[i]; j++) {
     cout << SYM[i];
   }

   //get the remainder
   n %= NUM[i];
 }
}