A palindrome is a string that reads the same forward and backward, such as \"rad
ID: 3547156 • Letter: A
Question
A palindrome is a string that reads the same forward and backward, such as "radar". Write a static recursive method that has one parameter of type String and returns true if the argument is a palindrome and false otherwise. Disregard spaces and punctuation marks in the string, and consider upper- and lowercase versions of the same letter to be equal. For example, the following strings should be considered palindromes by your method:
"Straw? No, too stupid a fad, I put soot on warts."
"xyzcZYx?"
Your method need not check that the string is a correct English phrase or word. Embed the method in a program, and test it.
Note: You can assume that the user wont enter any number. For simplicity, include your main method in the same file as your private recursive method and public interface method.
P.S. This is for Java.
I also will give review and rating. Thank you.
Explanation / Answer
import java.util.Scanner;
public class PolyndromTest {
public static void main(String[] args) {
Scanner inScanner = new Scanner(System.in);
System.out.println("Enter a string: ");
String text = inScanner.nextLine();
if (isPolindrome(text)) {
System.out.println(text + " is polindrome ");
} else {
System.out.println(text + " is not polindrome ");
}
}
private static boolean isPolindrome(String str) {
str = str.toUpperCase();
str = str.replaceAll("[^A-Za-z]", "");
if (str.length() == 0 || str.length() == 1)
return true;// if length =0 OR 1 then it is
else if (str.charAt(0) == str.charAt(str.length() - 1))
return isPolindrome(str.substring(1, str.length() - 1));
return false;
}
}
Output -I :
Enter a string:
madam
madam is polindrome
output-II
Enter a string:
Straw? No, too stupid a fad, I put soot on warts.
Straw? No, too stupid a fad, I put soot on warts. is polindrome
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.