Coding Challenge Day 1: Problem 1: Roman numeral to Integer

Coding challenge introduction

Question: Given a Roman numeral, the task is to find its corresponding decimal value. Here is the link to the details of the question

My Solution:

int romanToInt(char * s){
    int i=0, number=0;
    
    while(s[i]!='\0'){
        char current_char = s[i];
        switch(current_char){
            case 'I':
                if(s[i+1]=='V'){
                    number += 4;
                    i++;
                } else if (s[i+1] == 'X'){
                    number += 9;
                    i++;
                } else {
                    number += 1;   
                }
                break;
                
            case 'V':
                number += 5;
                break;
                
            case 'X':
                if(s[i+1]=='L'){
                    number += 40;
                    i++;
                } else if (s[i+1] == 'C'){
                    number += 90;
                    i++;
                } else {
                    number += 10;   
                }
                break;
                
            case 'L':
                number += 50;
                break;
                
            case 'C':
                if(s[i+1]=='D'){
                    number += 400;
                    i++;
                } else if (s[i+1] == 'M'){
                    number += 900;
                    i++;
                } else {
                    number += 100;   
                }
                break;
                
            case 'D':
                number += 500;
                break;
                
            case 'M':
                number += 1000;
                break;
        }
        i++;
    }
    
    return number;

}

Leave a Reply

PHP JS HTML CSS BASH PYTHON CODE

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: