Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@wperron
Created June 13, 2022 13:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wperron/4658160b6ebe3d4050430c2044f117d0 to your computer and use it in GitHub Desktop.
Save wperron/4658160b6ebe3d4050430c2044f117d0 to your computer and use it in GitHub Desktop.
Create a looooong teeeeext generator
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