From 31d1b65153d925ca1598cc3eacc8b61fc0cb0bf9 Mon Sep 17 00:00:00 2001 From: Divlo Date: Wed, 4 Jan 2023 07:50:35 +0100 Subject: [PATCH] feat(solutions): add `acronyms/rust/function` --- challenges/acronyms/README.md | 2 +- .../solutions/rust/function/Cargo.lock | 7 +++++++ .../solutions/rust/function/Cargo.toml | 6 ++++++ .../solutions/rust/function/README.md | 3 +++ .../solutions/rust/function/src/main.rs | 21 +++++++++++++++++++ 5 files changed, 38 insertions(+), 1 deletion(-) create mode 100644 challenges/acronyms/solutions/rust/function/Cargo.lock create mode 100644 challenges/acronyms/solutions/rust/function/Cargo.toml create mode 100644 challenges/acronyms/solutions/rust/function/README.md create mode 100644 challenges/acronyms/solutions/rust/function/src/main.rs diff --git a/challenges/acronyms/README.md b/challenges/acronyms/README.md index cb3b43a..4c8197a 100644 --- a/challenges/acronyms/README.md +++ b/challenges/acronyms/README.md @@ -10,7 +10,7 @@ Convert a given sentence to its acronym. ### Example 1 -#### Input +### Input ```txt Programming Challenges is really cool diff --git a/challenges/acronyms/solutions/rust/function/Cargo.lock b/challenges/acronyms/solutions/rust/function/Cargo.lock new file mode 100644 index 0000000..c8ae10e --- /dev/null +++ b/challenges/acronyms/solutions/rust/function/Cargo.lock @@ -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" diff --git a/challenges/acronyms/solutions/rust/function/Cargo.toml b/challenges/acronyms/solutions/rust/function/Cargo.toml new file mode 100644 index 0000000..a119ace --- /dev/null +++ b/challenges/acronyms/solutions/rust/function/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "rust_application" +version = "1.0.0" +edition = "2021" + +[dependencies] diff --git a/challenges/acronyms/solutions/rust/function/README.md b/challenges/acronyms/solutions/rust/function/README.md new file mode 100644 index 0000000..be930ae --- /dev/null +++ b/challenges/acronyms/solutions/rust/function/README.md @@ -0,0 +1,3 @@ +# acronyms/rust/function + +Created by [@Divlo](https://github.com/Divlo) on 4 January 2023. diff --git a/challenges/acronyms/solutions/rust/function/src/main.rs b/challenges/acronyms/solutions/rust/function/src/main.rs new file mode 100644 index 0000000..a2fa680 --- /dev/null +++ b/challenges/acronyms/solutions/rust/function/src/main.rs @@ -0,0 +1,21 @@ +use std::io; + +pub fn get_acronym(sentence: &str) -> String { + let words = sentence.replace("\"", ""); + let words = words.split_whitespace(); + let mut acronym = String::new(); + for word in words { + if let Some(character) = word.to_uppercase().chars().next() { + acronym.push(character); + } + } + acronym +} + +fn main() { + let mut input = String::new(); + io::stdin() + .read_line(&mut input) + .expect("Failed to read `stdin` line."); + println!("{}", get_acronym(&input)); +}