Pure Programmer
Blue Matrix


Cluster Map

Project: Starter

A [[palindrome]] is a word or phrase that is spelled the same when it is reversed. Write two functions that test a supplied string to see if it is a palindrome and return a boolean result. Ignore both case and punctuation. Writing a function to remove punctuation and convert to all lowercase would be helpful.

The first function should use recursion by checking the first and last characters to see if they are the same. If they are, pass the string, minus these characters, to the function again. Continue until there is one or no characters left.

The second function should not use recursion but only iteration to compute the same result.

Write a test program to test each function with the following to see if both produce the same results.

  • Hannah
  • Able was I ere I saw Elba
  • A man, a plan, a canal – Panama
  • T. Eliot, top bard, notes putrid tang emanating, is sad; I'd assign it a name: gnat dirt upset on drab pot toilet.
  • This is not a palindrome!

Output
$ rustc Palindrome.rs error[E0432]: unresolved import `regex` --> Palindrome.rs:8:5 | 8 | use regex::Regex; | ^^^^^ maybe a missing crate `regex`? | = help: consider adding `extern crate regex` to use the `regex` crate warning: unnecessary parentheses around function argument --> Palindrome.rs:20:42 | 20 | return palindromeRecursive0(&substr(s,1,(sLen - 1))); | ^ ^ | = note: `#[warn(unused_parens)]` on by default help: remove these parentheses | 20 - return palindromeRecursive0(&substr(s,1,(sLen - 1))); 20 + return palindromeRecursive0(&substr(s,1,sLen - 1)); | error[E0425]: cannot find function `strlen` in this scope --> Palindrome.rs:13:19 | 13 | let sLen:isize = strlen(s); | ^^^^^^ not found in this scope error[E0277]: the type `str` cannot be indexed by `{integer}` --> Palindrome.rs:17:7 | 17 | if s[0] != s[sLen - 1] { | ^ string indices are ranges of `usize` | = help: the trait `SliceIndex<str>` is not implemented for `{integer}` = note: you can use `.chars().nth()` or `.bytes().nth()` for more information, see chapter 8 in The Book: <https://doc.rust-lang.org/book/ch08-02-strings.html#indexing-into-strings> = help: the trait `SliceIndex<[T]>` is implemented for `usize` = note: required for `str` to implement `Index<{integer}>` error[E0425]: cannot find function `substr` in this scope --> Palindrome.rs:20:31 | 20 | return palindromeRecursive0(&substr(s,1,(sLen - 1))); | ^^^^^^ not found in this scope error[E0425]: cannot find function `replaceAll` in this scope --> Palindrome.rs:24:22 | 24 | let mut s0:String = replaceAll(s,Regex::new(r"\\W+").unwrap(),""); | ^^^^^^^^^^ not found in this scope error[E0425]: cannot find function `tolower` in this scope --> Palindrome.rs:25:7 | 25 | s0 = tolower(s0); | ^^^^^^^ not found in this scope error[E0425]: cannot find function `replaceAll` in this scope --> Palindrome.rs:30:22 | 30 | let mut s0:String = replaceAll(s,Regex::new(r"\\W+").unwrap(),""); | ^^^^^^^^^^ not found in this scope error[E0425]: cannot find function `tolower` in this scope --> Palindrome.rs:31:7 | 31 | s0 = tolower(s0); | ^^^^^^^ not found in this scope error[E0425]: cannot find function `strlen` in this scope --> Palindrome.rs:32:19 | 32 | let sLen:isize = strlen(s0); | ^^^^^^ not found in this scope error: aborting due to 9 previous errors; 1 warning emitted Some errors have detailed explanations: E0277, E0425, E0432. For more information about an error, try `rustc --explain E0277`.

Solution