Search code examples
c#integerroman-numerals

What is the best way to find out if a string is a roman number?


In almost all examples are about converting roman numbers to integer. Does it make sense to convert one by one all roman numbers and then check if that number is a greater than 0?


Solution

  • Not sure if you can find anything form the box. To create own function :

    private static Dictionary<char, int> _romanMap = new Dictionary<char, int>
    {
       {'I', 1}, {'V', 5}, {'X', 10}, {'L', 50}, {'C', 100}, {'D', 500}, {'M', 1000}
    };
    
    
    public static int IsRoman(string text) {
      foreach (var c in text) 
          if (!_romanMap.ContainsKey(char))
              return 0;
       return 1 
     }