(defn palindrome? [string]
;This function checks to see if string is a palindrome
(let [length (.length string)] ;Get length of the string
(if (or (= length 1) (= length 0)) ;Base case: if string is size 1 or 0, it's a palindrome
true)
(if (= (.charAt string 0) (.charAt string (- length 1))) ;Otherwise check the back and front and see if they match
(palindrome? (.substring string 1 (- length 2))) ;Recursively check the front and back letters
false))) ;Otherwise if they don't match, it's not a palindrome
;This code just makes a prompt and allows the user to input a string
;prints 'Yes' if it is a palindrome
;otherwise it prints 'No'
(print "=>")
(flush)
(if (is_palindrome (str (read)))
(println "Yes")
(println "No"))
When I put in the string "HellolleH" it prints 'No'. Can anyone point me into the right direction? Thank you.

New Topic/Question
Reply



MultiQuote




|