DEV Community

Discussion on: Daily Challenge #179 - Hide Phone Numbers

Collapse
 
idanarye profile image
Idan Arye

Rust:

fn encrypt_num(phone_number: String) -> String {
    let mut bytes = phone_number.into_bytes();
    for ch in bytes.iter_mut().rev().filter(|b| {
        let c = **b as char;
        '0' <= c && c <= '9'
    }).take(6) {
        *ch = 'X' as u8;
    }
    String::from_utf8(bytes).unwrap()
}

fn main() {
    assert_eq!(encrypt_num("328 6587120".to_owned()), "328 6XXXXXX");
    assert_eq!(encrypt_num("212-420-0202".to_owned()), "212-4XX-XXXX");
    assert_eq!(encrypt_num("211-458-7851".to_owned()), "211-4XX-XXXX");
}