* issue 108 example: rust palindrome program * edits on gitignore file * update to specification file * Updated README.md * Updated parent READMEpull/111/head
parent
cc756628a2
commit
e250c52ded
|
@ -25,4 +25,9 @@
|
||||||
|
|
||||||
### Python
|
### Python
|
||||||
|
|
||||||
1. [Palindrome Check](python/palindrome.py)
|
1. [Palindrome Check](python/palindrome.py)
|
||||||
|
|
||||||
|
### Rust
|
||||||
|
|
||||||
|
1. [Palindrome Check](rust/palindrome/README.md)
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,2 @@
|
||||||
|
# remove the path 'target' (for cargo builds) from git
|
||||||
|
target/
|
|
@ -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"
|
|
@ -0,0 +1,9 @@
|
||||||
|
[package]
|
||||||
|
name = "palindrome"
|
||||||
|
version = "1.0.0"
|
||||||
|
authors = ["Goutham Krishna K V <gauthamkrishna9991@live.com>"]
|
||||||
|
edition = "2018"
|
||||||
|
|
||||||
|
# Dependencies
|
||||||
|
|
||||||
|
[dependencies]
|
|
@ -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`
|
|
@ -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);
|
||||||
|
}
|
Loading…
Reference in New Issue