Ad

Make the string a list of separatedd strings

In this task I want you to take a string with ascii text and split it into a list of strings with all the words from the original string separated by a string containing a separator. Also given to you. For example:

input: string:"Lorem ipsum dolor sit amet", separator: '_'

output: ["Lorem", "_", "ipsum", "_", "dolor", "_", "sit", "_", "amet"]
fn separate_strings(list: &str, separator: char) -> Vec<String> {
    let mut res: Vec<String> = Vec::new();
    
    let words: Vec<&str> = list.split(" ").collect();
    
    let total = words.len();
    
    for (index, word) in words.iter().enumerate() {
        res.push(String::from(*word));
        if index < total - 1 {
            res.push(separator.to_string());
        }
    }
    
    res
}