From e250c52ded856a69116b1662cd1fed0b91623e1d Mon Sep 17 00:00:00 2001 From: Goutham Krishna Date: Tue, 16 Mar 2021 18:11:41 +0530 Subject: [PATCH] rust-> palindrome program, enforces specification in #108 (#109) * issue 108 example: rust palindrome program * edits on gitignore file * update to specification file * Updated README.md * Updated parent README --- strings/README.md | 7 +++++- strings/rust/palindrome/.gitignore | 2 ++ strings/rust/palindrome/Cargo.lock | 5 ++++ strings/rust/palindrome/Cargo.toml | 9 +++++++ strings/rust/palindrome/README.md | 22 +++++++++++++++++ strings/rust/palindrome/src/main.rs | 38 +++++++++++++++++++++++++++++ 6 files changed, 82 insertions(+), 1 deletion(-) create mode 100644 strings/rust/palindrome/.gitignore create mode 100644 strings/rust/palindrome/Cargo.lock create mode 100644 strings/rust/palindrome/Cargo.toml create mode 100644 strings/rust/palindrome/README.md create mode 100644 strings/rust/palindrome/src/main.rs diff --git a/strings/README.md b/strings/README.md index ef33c4a0..b8347c40 100644 --- a/strings/README.md +++ b/strings/README.md @@ -25,4 +25,9 @@ ### Python -1. [Palindrome Check](python/palindrome.py) \ No newline at end of file +1. [Palindrome Check](python/palindrome.py) + +### Rust + +1. [Palindrome Check](rust/palindrome/README.md) + diff --git a/strings/rust/palindrome/.gitignore b/strings/rust/palindrome/.gitignore new file mode 100644 index 00000000..a71860a0 --- /dev/null +++ b/strings/rust/palindrome/.gitignore @@ -0,0 +1,2 @@ +# remove the path 'target' (for cargo builds) from git +target/ diff --git a/strings/rust/palindrome/Cargo.lock b/strings/rust/palindrome/Cargo.lock new file mode 100644 index 00000000..5899183e --- /dev/null +++ b/strings/rust/palindrome/Cargo.lock @@ -0,0 +1,5 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "palindrome" +version = "1.0.0" diff --git a/strings/rust/palindrome/Cargo.toml b/strings/rust/palindrome/Cargo.toml new file mode 100644 index 00000000..59047ca5 --- /dev/null +++ b/strings/rust/palindrome/Cargo.toml @@ -0,0 +1,9 @@ +[package] +name = "palindrome" +version = "1.0.0" +authors = ["Goutham Krishna K V "] +edition = "2018" + +# Dependencies + +[dependencies] diff --git a/strings/rust/palindrome/README.md b/strings/rust/palindrome/README.md new file mode 100644 index 00000000..1766450b --- /dev/null +++ b/strings/rust/palindrome/README.md @@ -0,0 +1,22 @@ +# The Palindrome Algorithm + +This checks whether a string is palindrome or not and returns a result. + +## Prerequisites + +- Rust (https://rust-lang.org) + +## Instructions for running the project + +- Open a terminal/powershell window in the folder/directory. + +- Run the following command + + ```bash + cargo run --release + ``` + +## Test Cases & Output +- `abba` -> `true` +- `abbcccbba` -> `true` +- `abbccbbba` -> `false` diff --git a/strings/rust/palindrome/src/main.rs b/strings/rust/palindrome/src/main.rs new file mode 100644 index 00000000..3d7aa5f1 --- /dev/null +++ b/strings/rust/palindrome/src/main.rs @@ -0,0 +1,38 @@ +// The Palindrome Algorithm +// this takes in a string and returns a boolean equal to the result of +// whether the program is palindrome or not. +fn palindrome(s: &str) -> bool { + // char vector to keep the normal string + let string: Vec = s.chars().collect(); + // char vector to keep the reverse string + let mut reverse: Vec = Vec::new(); + // reverse the string and hold it in reverse. + for ch in &string { + reverse.insert(0, *ch); + } + // return whether the reverse and the normal matches + return string == reverse; +} + +// a utility function to check the result of palindrome function +fn check_palindrome(s: &str) { + if palindrome(s) { + println!("{} is a palindrome", s); + } else { + println!("{} is not a palindrome", s); + } +} + +// main function +fn main() { + // string 1 + let s1 = "abba"; + // string 2 + let s2 = "abbcccbba"; + // string 3 + let s3 = "abbccbbba"; + // call check_palindrome (internally calls palindrome) for each string + check_palindrome(s1); + check_palindrome(s2); + check_palindrome(s3); +}