C Solution :)


  • 0
    C

    Whats up my lads ;)

    To keep it simple, you will need some background on roman numerals so you may want to read this:
    http://www.math-only-math.com/rules-for-formation-of-roman-numerals.html

    Next here are some hints:

    • remember that after decimals your string will end with NULL = '\0'
    • start by picking a start character and figuring out what possible next characters can come after. (Don't forget to account for NULL)
    • rules scale for values that start with 1 (1,10,100,1000) and for values that start with 5 (5, 50, 500)
    • I left my printf statements to help some of you get an idea of how to sequentially debug this problem.

    Hope it helps and Good luck and stay sharp :) !!!

    int romanToInt(char* s) {
        int i = 0;
        int RomanToDecimal = 0;
        for(i=0; s[i] != '\0'; i++){ // steps through the entire string
            if (s[i]=='I'){
                if (s[i+1] == 'I' && s[i+2] == 'I' && s[i+3] == '\0' ){ //this is a 3
                    RomanToDecimal = RomanToDecimal + 3;
                    i= i+2;
                }
                else if (s[i+1] == 'I' && s[i+2] == '\0'){ // this is a 2
                    RomanToDecimal = RomanToDecimal + 2;
                    i= i+1;
                }
                else if (s[i+1] == 'V' && s[i+2] == '\0' ){ // this is a 4
                    RomanToDecimal = RomanToDecimal + 4;
                    i= i+1;
                }
                else if (s[i+1] == 'X' && s[i+2] == '\0' ){ // this is a 9
                    RomanToDecimal = RomanToDecimal + 9;
                    i= i+1;
                }
                else if (s[i+1] == '\0'){  // this is a 1
                    RomanToDecimal = RomanToDecimal + 1;
                }
    //            printf(" if 1 char = %c total = %d \n",s[i], RomanToDecimal);
            }
            else if (s[i] =='V'){
                if (s[i+1] == 'I' && s[i+2] == 'I' && s[i+3] == 'I' && s[i+4] == '\0' ){ //this is a 8
                    RomanToDecimal = RomanToDecimal + 8;
                    i= i+3;
                }
                else if (s[i+1] == 'I' && s[i+2] == 'I' && s[i+3] == '\0'){ // this is a 7
                    RomanToDecimal = RomanToDecimal + 7;
                    i= i+2;
                }
                else if (s[i+1] == 'I' && s[i+2] == '\0'){ // this is a 6
                    RomanToDecimal = RomanToDecimal + 6;
                    i= i+1;
                }
                else if (s[i+1] == '\0'){  // this is a 5
                    RomanToDecimal = RomanToDecimal + 5;
                }
    //            printf(" if 2 char = %c total = %d \n",s[i], RomanToDecimal);
            }
            else if (s[i] == 'X'){
                if (s[i+1] == 'X' && s[i+2] == 'X' ){ //this is a 30
                    RomanToDecimal = RomanToDecimal + 30;
                    i= i+2;
                }
                else if (s[i+1] == 'X'){ // this is a 20
                    RomanToDecimal = RomanToDecimal + 20;
                    i= i+1;
                }
                else if (s[i+1] == 'L' ){ // this is a 40
                    RomanToDecimal = RomanToDecimal + 40;
                    i= i+1;
                }
                else if (s[i+1] == 'C' ){ // this is a 90
                    RomanToDecimal = RomanToDecimal + 90;
                    i= i+1;
                }
                else {  // this is a 10
                    RomanToDecimal = RomanToDecimal + 10;
                }
    //            printf(" if 3 char = %c total = %d \n",s[i], RomanToDecimal);
                
            }
            else if (s[i] == 'L'){
                if (s[i+1] == 'X' && s[i+2] == 'X' && s[i+3] == 'X' ){ //this is a 80
                    RomanToDecimal = RomanToDecimal + 80;
                    i= i+3;
                }
                else if (s[i+1] == 'X' && s[i+2] == 'X' ){ // this is a 70
                    RomanToDecimal = RomanToDecimal + 70;
                    i= i+2;
                }
                else if (s[i+1] == 'X' ){ // this is a 60
                    RomanToDecimal = RomanToDecimal + 60;
                    i= i+1;
                }
                else {  // this is a 50
                    RomanToDecimal = RomanToDecimal + 50;
                }
    //            printf(" if 4 char = %c total = %d \n",s[i], RomanToDecimal);
                
            }
            else if (s[i] == 'C'){
                if (s[i+1] == 'C' && s[i+2] == 'C' ){ //this is a 300
                    RomanToDecimal = RomanToDecimal + 300;
                    i= i+2;
                }
                else if (s[i+1] == 'C'){ // this is a 200
                    RomanToDecimal = RomanToDecimal + 200;
                    i= i+1;
                }
                else if (s[i+1] == 'D' ){ // this is a 400
                    RomanToDecimal = RomanToDecimal + 400;
                    i= i+1;
                }
                else if (s[i+1] == 'M' ){ // this is a 900
                    RomanToDecimal = RomanToDecimal + 900;
                    i= i+1;
                }
                else {  // this is a 100
                    RomanToDecimal = RomanToDecimal + 100;
                }
    //            printf(" if 5 char = %c total = %d \n",s[i], RomanToDecimal);
            }
            else if (s[i] == 'D'){
                if (s[i+1] == 'C' && s[i+2] == 'C' && s[i+3] == 'C' ){ //this is a 800
                    RomanToDecimal = RomanToDecimal + 800;
                    i= i+3;
                }
                else if (s[i+1] == 'C' && s[i+2] == 'C' ){ // this is a 700
                    RomanToDecimal = RomanToDecimal + 700;
                    i= i+2;
                }
                else if (s[i+1] == 'C' ){ // this is a 600
                    RomanToDecimal = RomanToDecimal + 600;
                    i= i+1;
                }
                else {  // this is a 500
                    RomanToDecimal = RomanToDecimal + 500;
                }
    //            printf(" if 6 char = %c total = %d \n",s[i], RomanToDecimal);
            }
            else if (s[i] == 'M'){
                if (s[i+1] == 'M' && s[i+2] == 'M' ){ //this is a 3000
                    RomanToDecimal = RomanToDecimal + 3000;
                    i= i+2;
                }
                else if (s[i+1] == 'M'){ // this is a 2000
                    RomanToDecimal = RomanToDecimal + 2000;
                    i= i+1;
                }
                else {  // this is a 1000
                    RomanToDecimal = RomanToDecimal + 1000;
                }
    //            printf(" if 7 char = %c total = %d \n",s[i], RomanToDecimal);
            }
    //        printf("char = %c total = %d \n",s[i], RomanToDecimal);
        }
    //remember
    // I = 1
    // V = 5
    // X = 10
    // L = 50
    // C = 100
    // D = 500
    // M = 1000
        
        
        return RomanToDecimal;
        
    }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.