How to sort the words in a string with Rust

1 Answer

0 votes
fn sort_the_words_in_a_string(s: &str) -> String {
    let mut arr: Vec<&str> = s.split_whitespace().collect();
    arr.sort();

    let result = arr.join(" ");
    
    result
}

fn main() {
    let s = "php c rust java c++ python c#";
    
    let sorted_s = sort_the_words_in_a_string(s);
    
    println!("{}", sorted_s);
}




/*
run:

c c# c++ java php python rust

*/

 



answered Jul 29, 2024 by avibootz
...