Skip to content

Instantly share code, notes, and snippets.

@wperron
Created December 19, 2022 17:17
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/d18ac9030227309232225b79b22ab18f to your computer and use it in GitHub Desktop.
Save wperron/d18ac9030227309232225b79b22ab18f to your computer and use it in GitHub Desktop.
Given a string, make every consonant after a vowel uppercase.
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