mirror of
https://github.com/theoludwig/programming-challenges.git
synced 2025-05-18 12:02:53 +02:00
feat(solutions): add camel-case/rust/function
This commit is contained in:
32
challenges/camel-case/solutions/rust/function/src/main.rs
Normal file
32
challenges/camel-case/solutions/rust/function/src/main.rs
Normal file
@ -0,0 +1,32 @@
|
||||
use std::io;
|
||||
|
||||
pub fn capitalize(string: &str) -> String {
|
||||
let mut character = string.chars();
|
||||
match character.next() {
|
||||
None => String::new(),
|
||||
Some(value) => value.to_uppercase().collect::<String>() + character.as_str(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn camel_case(string: &str) -> String {
|
||||
let mut result: Vec<String> = Vec::new();
|
||||
let words: Vec<&str> = string.trim().split_whitespace().collect();
|
||||
for (index, word) in words.iter().enumerate() {
|
||||
let is_first_word = index == 0;
|
||||
if is_first_word {
|
||||
result.push(word.to_string());
|
||||
} else {
|
||||
let word = capitalize(&word);
|
||||
result.push(word);
|
||||
}
|
||||
}
|
||||
return result.join("");
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let mut string = String::new();
|
||||
io::stdin()
|
||||
.read_line(&mut string)
|
||||
.expect("Failed to read `stdin` line.");
|
||||
println!("{}", camel_case(&string));
|
||||
}
|
Reference in New Issue
Block a user