Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

A palindrome is a string that is the same forward and backward. In Chapter 5 you saw a program that uses a loop to determine

A palindrome is a string that is the same forward and backward. In Chapter 5 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.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Modern Database Management

Authors: Jeffrey A. Hoffer Fred R. McFadden

9th Edition

B01JXPZ7AK, 9780805360479

More Books

Students also viewed these Databases questions