Question: A palindrome is a string that is the same forward and backward. In Chapter 3 you saw a program that uses a loop to determine
A palindrome is a string that is the same forward and backward. In Chapter 3 you saw a program that uses a loop to determine whether a string is a palindrome. However, it is also easy to define a palindrome recursively as follows:
- A string containing fewer than 2 letters is always a palindrome.
- A string containing 2 or more letters is a palindrome if
- its first and last letters are the same, and
- the rest of the string (without the first and last letters) is also a palindrome.
Write a program that prompts for and reads in a string, then prints a message saying whether it is a palindrome. Your main method should read the string and call a recursive (static) method palindrome that takes a string and returns true if the string is a palindrome, false otherwise. Recall that for a String s in Java,
- s.length() returns the number of charaters in s
- s.charAt(i) returns the ith character of s, 0-based
- s.substring(i,j) returns the substring that starts with the ith character of s and ends with the j-1st character of s (not the jth!), both 0-based. So if s="happy", s.length=5, s.charAt(1)=a, and s.substring(2,4) = "pp".
this was all I was given to work with, please do not use anything past a beginners level of computer science so that I am able to understand it we are only on the chapter titled recursion, if you have any questions please ask
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
