How to get the reciprocal of the letters in a string with Rust

1 Answer

0 votes
fn get_reciprocal(s: &str) -> String {
    let mut tmp = String::new();
    
    for ch in s.chars() {
        if ch.is_ascii_uppercase() {
            tmp.push((b'Z' - (ch as u8 - b'A')) as char);
        } else if ch.is_ascii_lowercase() {
            tmp.push((b'z' - (ch as u8 - b'a')) as char);
        } else {
            tmp.push(ch);
        }
    }
    tmp
}

fn main() {
    let s = "abc++def";
    
    let s = get_reciprocal(s);
    
    println!("{}", s);
}

     
     
/*
run:
 
zyx++wvu
 
*/

 

 



answered Oct 16, 2024 by avibootz

Related questions

1 answer 104 views
1 answer 99 views
1 answer 89 views
1 answer 91 views
1 answer 98 views
...