Roman Numeral in Swift

Published on 03 May 2026 (Updated: 03 May 2026)

Welcome to the Roman Numeral in Swift page! Here, you'll find the source code for this program as well as a description of how the program works.

Current Solution

import Foundation

let romanValues: [Character: Int] = [
    "I": 1,
    "V": 5,
    "X": 10,
    "L": 50,
    "C": 100,
    "D": 500,
    "M": 1000,
]

let usage = "Usage: please provide a string of roman numerals"
let error = "Error: invalid string of roman numerals"

func romanToInt(_ s: String) -> Int? {
    var total = 0
    var previous = 0

    for char in s.uppercased().reversed() {
        guard let value = romanValues[char] else { return nil }

        if value < previous {
            total -= value
        } else {
            total += value
            previous = value
        }
    }

    return total
}

guard let input = CommandLine.arguments.dropFirst().first else {
    print(usage)
    exit(1)
}

guard let result = romanToInt(input) else {
    print(error)
    exit(1)
}

print(result)

Roman Numeral in Swift was written by:

If you see anything you'd like to change or update, please consider contributing.

How to Implement the Solution

No 'How to Implement the Solution' section available. Please consider contributing.

How to Run the Solution

No 'How to Run the Solution' section available. Please consider contributing.