String replacement

Hi
I am new to prgramming. At the moment I am trying to Convert roman numerals to arabic. I want to replace IV with IIII , IX with I(9 times), XL with XXXX and etc. I have trouble with changing two characters with multiple characters. Can anyone help me?
I suggest you provide the code showing what you've tried.
I actually tried different algorithm, but know I am thinking about this one. Here is my previous code. Hope you will understand it

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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149

#include<stdio.h>
#include<stdlib.h>
#include<string.h>

int main(){
	/*
	
	I - 1
	V - 5
	X - 10
	L - 50
	C - 100
	D - 500
	M - 1000
	
	I subtracted from V and X 
	X subtracted from L and C
	C subtracted from D and M
	
	V,L,D can never be subtracted
	
	*/
	
	
	int i;
	int I_say=0,V_say=0,X_say=0,L_say=0,C_say=0,D_say=0,M_say=0; // # OF OCCURENCE
	int i_=0,x_=0,c_=0;                                          // # of characters, will be used FOR SUBTRACTION
	char line[50];
	gets(line);
	// input sampe ->  IX+XL
	// output          XLIX

	for(i=0; line[i]!='\0'; i++){
		                                  //  IV=4    if (line[i]='I' and line[i+1]='V')
										  //  IX=9    if (line[i]='I' and line[i+1]='X')
										  //  XL=40   if (line[i]='X' and line[i+1]='L')
										  //  XC=90   if (line[i]='X' and line[i+1]='C')
										  //  CD=400  if (line[i]='C' and line[i+1]='D')
										  //  CM=900  if (line[i]='C' and line[i+1]='M')
										  
										  
		
		if(line[i]=='I'){
			if(line[i+1]=='V')
              i_++;                         
			else if(line[i+1]=='X')
			  i_++;
		    else
		      I_say++;
	    }
	    
	    
	    
		else if(line[i]=='V'){
		       if(line[i-1]=='I'){
		          V_say++;
		          i_++;	
		         }
		         else{
		         	V_say++;
				 }
		   }
		   
		   
		
		else if(line[i]=='X'){
			if(line[i-1]=='I'){   
		         X_say++;
			     i_++;		
			    }
			else{
				X_say++;
			}
		}
		
		
		
		else if(line[i]=='L'){
			if(line[i-1]=='X'){
				L_say++;
				x_++;
			}
			else{
				L_say++;
			}
		}
		
		
		
		
		
		else if(line[i]=='C'){
		     if(line[i-1]=='X'){
				C_say++;
				x_++;
			}
			else{
				C_say++;
			}
		} 
		
		
		
		
		
		else if(line[i]=='D'){
		   if(line[i-1]=='C'){
				D_say++;
				c_++;
			}
			else{
				D_say++;
			}
		}
		
		
		
		
		else if(line[i]=='M')
	       if(line[i-1]=='C'){
				M_say++;
				c_++;
			}
			else{
				M_say++;
			}
	}
	
	
	
	printf("I = %d\n",I_say);
	printf("V = %d\n",V_say);
	printf("X = %d\n",X_say);
	printf("L = %d\n",L_say);
	printf("C = %d\n",C_say);
	printf("D = %d\n",D_say);
	printf("M = %d\n",M_say);
	
	printf("\n");
	
	
	printf("-I = %d\n",i_);
	printf("-X = %d\n",x_);
	printf("-C = %d\n",c_);
	
	int cem = M_say*1000 + D_say*500 + C_say*100 + L_say*50 + X_say*10 + V_say*5 + I_say-i_-(x_*10)-(c_*100);
	printf("Cem = %d\n",cem);
Last edited on
Could you please go back and reformat your code using code tags.

"How to use code tags"
http://www.cplusplus.com/articles/jEywvCM9/

1
2
3
4
5
6
7
8
9
10
// little C++ Shell cog wheel button ---->
// (if missing try refreshing your browser)
#include <iostream>
using namespace std;

int main() {
    cout << "Hello code tags!\n";

    return 0;
}


Andy

PS the little 'cog' button even allows you to run your code on the C++ Shell web site)
Last edited on
Topic archived. No new replies allowed.