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
pull/111/head
Goutham Krishna 2021-03-16 18:11:41 +05:30 committed by GitHub
parent cc756628a2
commit e250c52ded
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 82 additions and 1 deletions

View File

@ -26,3 +26,8 @@
### Python
1. [Palindrome Check](python/palindrome.py)
### Rust
1. [Palindrome Check](rust/palindrome/README.md)

View File

@ -0,0 +1,2 @@
# remove the path 'target' (for cargo builds) from git
target/

View File

@ -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"

View File

@ -0,0 +1,9 @@
[package]
name = "palindrome"
version = "1.0.0"
authors = ["Goutham Krishna K V <gauthamkrishna9991@live.com>"]
edition = "2018"
# Dependencies
[dependencies]

View File

@ -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`

View File

@ -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<char> = s.chars().collect();
// char vector to keep the reverse string
let mut reverse: Vec<char> = 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);
}