Skip to content

Commit b292148

Browse files
committed
Added count_vovels in string. (saw it in the python version)
1 parent 4e25e99 commit b292148

File tree

2 files changed

+35
-0
lines changed

2 files changed

+35
-0
lines changed

src/string/count_vovels.rs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/// Counts the number of vowels in a given string.
2+
/// Vowels are defined as 'a', 'e', 'i', 'o', 'u' (both uppercase and lowercase).
3+
pub fn count_vowels(input: &str) -> usize {
4+
input.chars().filter(|c| "aeiouAEIOU".contains(*c)).count()
5+
}
6+
7+
#[cfg(test)]
8+
mod tests {
9+
use super::*;
10+
11+
macro_rules! test_count_vowels {
12+
($($name:ident: $tc:expr,)*) => {
13+
$(
14+
#[test]
15+
fn $name() {
16+
let (input, expected) = $tc;
17+
assert_eq!(count_vowels(input), expected);
18+
}
19+
)*
20+
}
21+
}
22+
23+
test_count_vowels! {
24+
empty_string: ("", 0),
25+
no_vowels: ("ghfdryfs", 0),
26+
all_vowels_lowercase: ("aeiou", 5),
27+
all_vowels_uppercase: ("AEIOU", 5),
28+
mixed_vowels: ("aEIoU", 5),
29+
hello_world: ("Hello, World!", 3),
30+
long_string: (&"a".repeat(1000), 1000),
31+
string_with_special_chars: ("!@%^&+aei*()_o#$u", 5),
32+
}
33+
}

src/string/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ mod anagram;
33
mod autocomplete_using_trie;
44
mod boyer_moore_search;
55
mod burrows_wheeler_transform;
6+
mod count_vowels;
67
mod duval_algorithm;
78
mod hamming_distance;
89
mod isogram;
@@ -30,6 +31,7 @@ pub use self::boyer_moore_search::boyer_moore_search;
3031
pub use self::burrows_wheeler_transform::{
3132
burrows_wheeler_transform, inv_burrows_wheeler_transform,
3233
};
34+
pub use self::count_vowels::count_vowels;
3335
pub use self::duval_algorithm::duval_algorithm;
3436
pub use self::hamming_distance::hamming_distance;
3537
pub use self::isogram::is_isogram;

0 commit comments

Comments
 (0)