-
Notifications
You must be signed in to change notification settings - Fork 0
/
romantointeger.c
73 lines (63 loc) · 1.4 KB
/
romantointeger.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
int romanToInt(char* s) {
// convert a roman numeral to a regular integer
int sum = 0;
// loop for each item in the string
for(int i = 0; i< sizeof(s); i++){
// case I
if (s[i] == 'I'){
if (s[i + 1] == 'V'){
sum--;
}
else if(s[i + 1] == 'X'){
sum--;
}
else{
sum++;
}
}
// case V
if(s[i] == 'V'){
sum+=5;
}
// case X
if(s[i] == 'X'){
if (s[i + 1] == 'L'){
sum-=10;
}
else if(s[i + 1] == 'C'){
sum-=10;
}
else{
sum+=10;
}
}
// case L
if(s[i] == 'L'){
sum+=50;
}
// case C
if(s[i] == 'C'){
if (s[i + 1] == 'D'){
sum-=100;
}
else if(s[i + 1] == 'M'){
sum-=100;
}
else{
sum+=100;
}
}
// case D
if(s[i] == 'D'){
sum+=500;
}
// case M
if(s[i] == 'M'){
sum+=1000;
}
if(s[i] == '\0'){
break;
}
}
return sum;
}