DEV Community

Discussion on: Daily Challenge #15 - Stop gninnipS My sdroW!

Collapse
 
qm3ster profile image
Mihail Malo • Edited

Rust, sort of in place

fn reverse_in_place(input: &mut [u8]) {
    input
        .split_mut(|&byte| byte == b' ')
        .filter(|word| word.len() >= 5)
        .for_each(<[_]>::reverse);
}

The rest of the story below the fold:

fn reverse(input: &str) -> String {
    let mut output = input.as_bytes().to_vec();
    reverse_in_place(&mut output);
    String::from_utf8(output).unwrap()
}

fn main() {
    let test = "Thank you to CodeWars, who has licensed redistribution of this challenge under the 2-Clause BSD License!";
    println!("{}", reverse(test));
    println!("{}", reverse(&reverse(test)));
}