1
1
mirror of https://github.com/theoludwig/programming-challenges.git synced 2024-12-08 00:45:29 +01:00

feat(solutions): add camel-case/rust/function

This commit is contained in:
Divlo 2023-01-04 17:40:50 +01:00
parent 431253da57
commit 791b1a7d52
No known key found for this signature in database
GPG Key ID: 8F9478F220CE65E9
5 changed files with 49 additions and 1 deletions

View File

@ -6,7 +6,7 @@ Created by [@Divlo](https://github.com/Divlo) on 5 July 2020.
Write a simple camelCase function for strings. All words (except the first) must have their first letter capitalized without spaces. Write a simple camelCase function for strings. All words (except the first) must have their first letter capitalized without spaces.
**Note :** camelCase is the practice of writing phrases such that each word in the middle of the phrase begins with a capital letter, with no intervening spaces or punctuation. **Note:** camelCase is the practice of writing phrases such that each word in the middle of the phrase begins with a capital letter, with no intervening spaces or punctuation.
## Examples ## Examples

View File

@ -0,0 +1,7 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 3
[[package]]
name = "rust_application"
version = "1.0.0"

View File

@ -0,0 +1,6 @@
[package]
name = "rust_application"
version = "1.0.0"
edition = "2021"
[dependencies]

View File

@ -0,0 +1,3 @@
# camel-case/rust/function
Created by [@Divlo](https://github.com/Divlo) on 4 January 2023.

View 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));
}