Created
December 19, 2022 17:17
-
-
Save wperron/d18ac9030227309232225b79b22ab18f to your computer and use it in GitHub Desktop.
Given a string, make every consonant after a vowel uppercase.
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
fn main() { | |
println!("{}", capitalize_post_vowels("Hello, World!".to_string())); | |
} | |
/// Given a string, make every consonant after a vowel uppercase. | |
fn capitalize_post_vowels(s: String) -> String { | |
let mut prev = false; | |
s.chars() | |
.into_iter() | |
.map(|c| match c { | |
'a' | 'e' | 'i' | 'o' | 'u' | 'A' | 'E' | 'I' | 'O' | 'U' => { | |
prev = true; | |
c | |
} | |
c if c.is_ascii_alphabetic() => { | |
let mut c = c; | |
if prev { | |
c = c | |
.to_uppercase() | |
.to_string() | |
.chars() | |
.nth(0) | |
.expect("failed to convert to char"); | |
} | |
prev = false; | |
c | |
} | |
c => c, | |
}) | |
.collect() | |
} | |
#[cfg(test)] | |
mod tests { | |
use super::*; | |
#[test] | |
fn test_capitalize_post_vowels() { | |
assert_eq!( | |
"heLlo WoRld".to_string(), | |
capitalize_post_vowels("hello world".to_string()) | |
); | |
assert_eq!( | |
"xaaBeueKaDii".to_string(), | |
capitalize_post_vowels("xaabeuekadii".to_string()) | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment