Complete the code to return the integer value of the Roman numeral 'I'.
int romanToInt(char * s) {
if (s[0] == 'I') {
return [1];
}
return 0;
}The Roman numeral 'I' equals 1, so the function should return 1.
Complete the code to get the integer value of a single Roman numeral character.
int value(char r) {
switch(r) {
case 'I': return 1;
case 'V': return 5;
case 'X': return 10;
case 'L': return 50;
case 'C': return 100;
case 'D': return 500;
case 'M': return 1000;
default: return [1];
}
}If the character is not a valid Roman numeral, returning 0 is safe to indicate no value.
Fix the error in the loop condition to correctly iterate over the string until the end.
int romanToInt(char * s) {
int total = 0;
int i = 0;
while (s[i] [1] '\0') {
total += value(s[i]);
i++;
}
return total;
}The loop should continue while the current character is not the null character '\0'.
Fill both blanks to correctly subtract when a smaller numeral precedes a larger one.
int romanToInt(char * s) {
int total = 0;
int i = 0;
while (s[i] != '\0') {
int s1 = value(s[i]);
if (s[i + 1] != '\0') {
int s2 = value(s[i + 1]);
if (s1 [1] s2) {
total += s2 - s1;
i += [2];
} else {
total += s1;
i++;
}
} else {
total += s1;
i++;
}
}
return total;
}If the current value is less than the next, subtract current from next and move two steps ahead.
Fill all three blanks to create a dictionary-like structure mapping Roman numerals to integers using arrays.
typedef struct {
char symbol;
int value;
} RomanMap;
RomanMap map[] = {
{'I', [1],
{'V', [2],
{'X', [3]
};The Roman numerals 'I', 'V', and 'X' correspond to 1, 5, and 10 respectively.
