QUOTE(Amadeus @ 27 Jul, 2007 - 06:12 PM)

I was thinking more along the lines of:
CODE
char str1[10];
printf("Please enter a Roman Numeral\n");
scanf("%s",str1);
printf("The string you entered was %s, and it has %d characters.\n",str1,strlen(str1));
You now have the roman numeral in a single character array, and can check any of the characters, including the next one.
Ok,
I have this codes, but it is in the reverse. From Decimal to Roman. Could you be kind enough to make it perform the other way round. From Roman to decimal? I will be glad you do. Thanks.
smartC
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
unsigned int roman_ranges[] = { 1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4,
1, 0 };
unsigned int roman_addsin[] = {-1000, +100, -500, +100, -100, +10, -50, +10, -10, +1, -5, +1,
-1, 0 };
char roman_letters[] = { 'M', 'C', 'D', 'C', 'C', 'X', 'L', 'X', 'X', 'I', 'V', 'I',
'I', 0 };
size_t find_index(unsigned int li) {
unsigned int *roman_ranges_ptr = roman_ranges;
while (*roman_ranges_ptr && li < *roman_ranges_ptr) {
++roman_ranges_ptr;
}
return roman_ranges_ptr - roman_ranges;
}
char* de2roman(char* buf, unsigned int li, size_t bufsize) {
size_t index;
if (bufsize < 1) {
return NULL; // buffer size is not enough for convertion
}
if ( (*buf = roman_letters[ (index = find_index(li)) ]) ) {
return de2roman(buf+1, li + roman_addsin[index], bufsize-1);
}
return buf;
}
#define ROMAN_NUMBER_BUF_SIZE 64
int main() {
unsigned int nr;
char roman_str[ROMAN_NUMBER_BUF_SIZE];
/* printf("%s", "Enter an integer: ");
scanf("%s", roman_str);*/
// while((nr = fgetc(stdin)) != '\n'){
while((scanf("%s", roman_str)) != '\n'){
de2roman(roman_str, nr, ROMAN_NUMBER_BUF_SIZE);
printf(" %d\n", nr);
}
return 0;
}
This post has been edited by smartC: 28 Jul, 2007 - 02:16 AM