Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Write a method that takes a string and returns true if it is a
- # palindrome. A palindrome is a string that is the same whether written
- # backward or forward. Assume that there are no spaces; only lowercase
- # letters will be given.
- #
- # Difficulty: easy.
- def palindrome?(string)
- reverse=""
- i=0
- while i<string.length
- reverse=string[i]+reverse
- i+=1
- end
- if reverse == string
- return true
- else return false
- end
- end
- # These are tests to check that your code is working. After writing
- # your solution, they should all print true.
- puts("\nTests for #palindrome?")
- puts("===============================================")
- puts('palindrome?("abc") == false: ' + (palindrome?('abc') == false).to_s)
- puts('palindrome?("abcba") == true: ' + (palindrome?('abcba') == true).to_s)
- puts('palindrome?("z") == true: ' + (palindrome?('z') == true).to_s)
- puts("===============================================")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement