-
Notifications
You must be signed in to change notification settings - Fork 33
Open
Description
Hi!
- Prepend
srto a word if it begins with a vowel. - Append
rsto a word if it begins with a consonant.
In the "Task" paragraph it is said that the sentence:
Implement a function that splits a sentence into its words
should be transformed into:
srImplement sra functionrs thatrs splitsrs sra sentencers srinto srits wordsre
(By the way, there's a typo in the last word: wordsre should be wordsrs).
If we run the solution using the previous sentence:
#![allow(unused)]
const VOWELS: [char; 5] = ['a', 'e', 'i', 'o', 'u'];
fn rustlatin(sentence: &str) -> String {
let mut collection_of_words = Vec::new();
for word in sentence.split(' ') {
let latinized_word = latinize(word);
collection_of_words.push(latinized_word)
}
collection_of_words.join(" ")
}
fn latinize(word: &str) -> String {
let first_char = word.chars().next().unwrap();
if VOWELS.contains(&first_char) {
let mut result = "sr".to_string();
result.push_str(word);
result
} else {
let mut result = word.to_string();
result.push_str("rs");
result
}
}
fn main() {
let sentence = "Implement a function that splits a sentence into its words";
println!("{:?}", rustlatin(sentence));
}we can see that the first word Implement will be turned into Implementrs instead of srImplement. This because capital vowels are not taken into account.
Some solutions
- Add the capital vowels to VOWELS constant:
so that it becomes:
const VOWELS: [char; 5] = ['a', 'e', 'i', 'o', 'u'];
const VOWELS: [char; 10] = ['a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U'];
- When initializing first_char use
to_lowercase(). From:to:let first_char = word.chars().next().unwrap();let first_char = word.to_lowercase().chars().next().unwrap(); - ...or make clear the exercise is intended for lowercase letters only, changing the sentence in the "Task" paragraph 😉
Metadata
Metadata
Assignees
Labels
No labels