import java.util.Scanner; public class Palindrome { public static void main (Str
ID: 3631660 • Letter: I
Question
import java.util.Scanner;
public class Palindrome
{
public static void main (String[]args)
{
Scanner in = new Scanner(System.in);
System.out.println("Enter a word: ");
String s = in.nextLine();
s = s.replaceAll("\W","");
s = s.toLowerCase();
if(PalindromeCalculator(s))
System.out.println(s + " is a Palindrome.");
else
System.out.println(s + " is not a Palindrome.");
}
public static boolean PalindromeCalculator(String s)
{
if (s.length() <= 1)
return true; // Base case
else
{
if (s.charAt(0) == s.charAt(s.length() - 1))
return PalindromeCalculator(s.substring(1, s.length() - 1 ) );
else
return false;
}//end PalindromeCalculator method
}//end main
}//end class
I need some one to explain this code and the math behind it. Adding comments to each section would be ideal. Thanks yo.
Explanation / Answer
Hope this may help you --------------------------------------------------- import java.util.Scanner; public class Palindrome { public static void main (String[]args) { Scanner in = new Scanner(System.in); System.out.println("Enter a word: "); String s = in.nextLine(); s = s.replaceAll("\W","");// replacing all non alphanumeric alphabet with empty string s = s.toLowerCase();// converting the string in lowercase. if(PalindromeCalculator(s))//calling the function PalindromeCalculator System.out.println(s + " is a Palindrome."); else System.out.println(s + " is not a Palindrome."); } public static boolean PalindromeCalculator(String s)//definatin of PalindromeCalculator function { if (s.length()Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.