I have a question regarding this
我对此有疑问
public class Palindrom
{
public static boolean isPalindrom(String text)
{
boolean back = true; //return variable
text = text.replaceAll("\s","");
//checking special cases - text.length() = 0 or 1
if (text.length() <= 1)
{
if (text.length() == 0)
{
System.out.println("An empty string cannot be a palindrom");
back = false;
}
else
{
//checking if the single String-element is a letter
if (Character.isLetter(text.charAt(0)) == true)
{
System.out.println("An single-character string - given that it is a valid word - is always a palindrom");
back = true;
}
else
{
System.out.println("The String contains something that isn't a letter 1");
back = false;
}
}
}
else
{
text = text.toLowerCase();
//now we always have to check if the first and last element are letters and if they're equal
if ((Character.isLetter(text.charAt(0)) && Character.isLetter(text.charAt(text.length() - 1))) == true)
{
//TEST
//System.out.println("Checks if letters");
if (text.charAt(0) == text.charAt(text.length() - 1))
{
back = true;
//now we have to the recursive calling
text = text.substring(1,text.length() - 1);
//if the cut String has a length = 0, then there is no need to call the method again
if (text.length() != 0)
{
//TEST
System.out.println("Calls the method again");
Palindrom.isPalindrom(text);
}
//else: we're finished
}
else
{
back = false;
}
}
else
{
System.out.println("The String contains something that isn't a letter 2");
back = false;
}
}
System.out.println("four");
System.out.println("back is : " + back);
System.out.println("five");
return back;
}
}
pub