Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions src/string/count_vovels.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/// Counts the number of vowels in a given string.
/// Vowels are defined as 'a', 'e', 'i', 'o', 'u' (both uppercase and lowercase).
pub fn count_vowels(input: &str) -> usize {
input.chars().filter(|c| "aeiouAEIOU".contains(*c)).count()
}

#[cfg(test)]
mod tests {
use super::*;

macro_rules! test_count_vowels {
($($name:ident: $tc:expr,)*) => {
$(
#[test]
fn $name() {
let (input, expected) = $tc;
assert_eq!(count_vowels(input), expected);
}
)*
}
}

test_count_vowels! {
empty_string: ("", 0),
no_vowels: ("ghfdryfs", 0),
all_vowels_lowercase: ("aeiou", 5),
all_vowels_uppercase: ("AEIOU", 5),
mixed_vowels: ("aEIoU", 5),
hello_world: ("Hello, World!", 3),
long_string: (&"a".repeat(1000), 1000),
string_with_special_chars: ("!@%^&+aei*()_o#$u", 5),
}
}
2 changes: 2 additions & 0 deletions src/string/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ mod anagram;
mod autocomplete_using_trie;
mod boyer_moore_search;
mod burrows_wheeler_transform;
mod count_vowels;
mod duval_algorithm;
mod hamming_distance;
mod isogram;
Expand Down Expand Up @@ -30,6 +31,7 @@ pub use self::boyer_moore_search::boyer_moore_search;
pub use self::burrows_wheeler_transform::{
burrows_wheeler_transform, inv_burrows_wheeler_transform,
};
pub use self::count_vowels::count_vowels;
pub use self::duval_algorithm::duval_algorithm;
pub use self::hamming_distance::hamming_distance;
pub use self::isogram::is_isogram;
Expand Down
Loading