Created
June 13, 2022 13:16
-
-
Save wperron/4658160b6ebe3d4050430c2044f117d0 to your computer and use it in GitHub Desktop.
Create a looooong teeeeext generator
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use std::iter::repeat; | |
fn main() { | |
println!("{}", long_string(String::from("hello world"), 3)); | |
println!("{}", long_string(String::from("lol"), 10)); | |
} | |
/// Create a loooong teeeext generator that takes in a string and an integer `n`, | |
/// and multiplies the vowels in the string by `n`. | |
/// | |
/// Example: | |
/// | |
/// ``` | |
/// let long = long_string(String::from("hello world"), 3) | |
/// // long == "heeellooo wooorld" | |
/// | |
/// let long = long_string(String::from("lol"), 10) | |
/// // long == "looooooooool" | |
/// ``` | |
fn long_string(s: String, n: usize) -> String { | |
let mut r = String::new(); | |
for c in s.chars() { | |
match c { | |
'a' | 'e' | 'i' | 'o' | 'u' => { | |
for repeated in repeat(c).take(n) { | |
r.push(repeated) | |
} | |
} | |
_ => r.push(c), | |
} | |
} | |
r | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment