Palindromes A palindrome is a string that is the same forward and backward. In C
ID: 3662609 • Letter: P
Question
Palindromes
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".
Printing a String Backwards
Printing a string backwards can be done iteratively or recursively. To do it recursively, think of the following specification:
If s contains any characters (i.e., is not the empty string)
print the last character in s
print s' backwards, where s' is s without its last character
File Backwards.java contains a program that prompts the user for a string, then calls method printBackwards to print the string backwards. Save this file to your directory and fill in the code for printBackwards using the recursive strategy outlined above.
// ******************************************************************
// Backwards.java
//
// Uses a recursive method to print a string backwards.
// ******************************************************************
import cs1.Keyboard;
public class Backwards
{
//--------------------------------------------------------------
// Reads a string from the user and prints it backwards.
//--------------------------------------------------------------
public static void main(String[] args)
{
String msg;
System.out.print("Enter a string: ");
msg = Keyboard.readString();
System.out.print(" The string backwards: ");
printBackwards(msg);
System.out.println();
}
//--------------------------------------------------------------
// Takes a string and recursively prints it backwards.
//--------------------------------------------------------------
public static void printBackwards(String s)
{
// Fill in code
}
}
Explanation / Answer
package assignment;
import java.util.Scanner;
public class PalindormeRecursive {
public static void main(String[] args)
{
String msg;
Scanner keyboard = new Scanner(System.in);
System.out.print("Enter a string: ");
msg = keyboard.nextLine();
System.out.print(" The string backwards: ");
printBackwards(msg);
System.out.println(" Is Polindrome: "+isPollindromRecur(msg));
}
public static boolean isPollindromRecur(String s) {
if(s != null) {
if(s.length() == 1)
return true;
else if(s.length() > 1) {
if(s.charAt(0) == s.charAt(s.length()-1))
return isPollindromRecur(s.substring(1,s.length()-1));
else
return false;
}
}
return false;
}
//--------------------------------------------------------------
// Takes a string and recursively prints it backwards.
//--------------------------------------------------------------
public static void printBackwards(String s)
{
// Fill in code
if(s != null && s.length() >= 1) {
System.out.print(s.charAt(s.length()-1));
if(s.length() != 1)
printBackwards(s.substring(0,s.length()-1));
} else
System.out.println();
}
}
----output---
Enter a string: madam
The string backwards: madam
Is Polindrome: true
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.