The last test case of s = "MCMXCIV" is not working. the output is 3099 instead of 1994. I have added all the roman numerals to integers and can't figure out where the issue is. The first two cases were properly executed to give expected output.
// code start
int romanToInt(char *s) {
int sum = 0;
if (*s == NULL) {
return NULL;
}
for (int i = 0; i < strlen(s); i++) {
if (s[i] == 'M') {
sum = sum + 1000;
}
if (s[i] == 'D') {
sum = sum + 500;
}
if (s[i] == 'C') {
sum = sum + 100;
}
if (s[i] == 'L') {
sum = sum + 50;
}
if (s[i] == 'X') {
sum = sum + 10;
}
if (s[i] == 'V') {
sum = sum + 5;
}
if (s[i] == 'I') {
sum = sum + 1;
}
if (i > 0) {
if (s[i] == 'V' && s[i - 1] == 'I') {
sum = sum + 3;
}
if (s[i] == 'X' && s[i - 1] == 'I') {
sum = sum + 8;
}
if (s[i] == 'L' && s[i - 1] == 'X') {
sum = sum + 30;
}
if (s[i] == 'C' && s[i - 1] == 'X') {
sum = sum + 80;
}
if (s[i] == 'D' && s[i - 1] == 'C') {
sum = sum + 300;
}
if (s[i] == 'M' && s[i - 1] == 'C') {
sum = sum + 800;
}
}
//sum represents the converted integer
}
return sum;
}//code end
There are multiple problems:
comparing
*stoNULLis incorrect:NULLis a macro representing a null pointer,*sis not a pointer, it is a character, which you can compare to'\0'or0instead. Your code compiles by chance ifNULLis defined as0or0Lbut would not for the alternative classic definition((void *)0).this initial test is useless anyway as the
forloop will stop immediately for an empty string.the test
i < strlen(s)may recompute the string length at each iteration, which is inefficient. You could just test ifs[i] != '\0'.the rule for roman numerals is simple: if a letter value is less than that of the next letter, its value is subtracted from the value of the next one.
Here is a modified version:
This simplistic implementation works for
MCMXCIV(1994) but does not detect invalid numbers such asMXMIV,MVMI,XMMIV... which will be all produce1994as well. The classic representations only useI,XandCin front of the next 2 letters, but many variations have been found throughout the long history of Roman Numerals.Roman numerals embody one of the most durable human inventions of all time: counting. The shape of the letters
I,VandXlook amazingly similar to Tally marks found on prehistoric artifacts. It gives me the chills to see in the most scientifically advanced publications the very shapes used by stone age cavemen to count.