TheAlgorithms-C/leetcode/src/13.c
dedsinQ 2215758b3b - Adding Roman to Integer (13.c)
- Adding Divide Two Integers (29.c)
- Adding Plus One (66.c)
- Adding Bitwise AND of Numbers Range (201.c)
- Readme updated
2019-10-26 16:06:02 +02:00

49 lines
1.4 KiB
C

int romanToInt(char * s){
int romanToInt = 0;
for (int i = 0; i < strlen(s); i++) {
switch(s[i]) {
case 'I':
if (i+1 < strlen(s)) {
if (s[i + 1] == 'V' || s[i + 1] == 'X') {
romanToInt -= 1;
break;
}
}
romanToInt += 1;
break;
case 'V':
romanToInt += 5;
break;
case 'X':
if (i+1 < strlen(s)) {
if (s[i + 1] == 'L' || s[i + 1] == 'C') {
romanToInt -= 10;
break;
}
}
romanToInt += 10;
break;
case 'L':
romanToInt += 50;
break;
case 'C':
if (i+1 < strlen(s)) {
if (s[i + 1] == 'D' || s[i + 1] == 'M') {
romanToInt -= 100;
break;
}
}
romanToInt += 100;
break;
case 'D':
romanToInt += 500;
break;
case 'M':
romanToInt += 1000;
break;
default:
break;
}
}
return romanToInt;
}