/home/wpollock1/public_html/Java/Palindrome.java
// This application checks the command line arguments to see if they
// form a palindrome: text that reads the same forward or backward.
// When checking, spaces and punctuation marks are ignored, and so
// is the case of the text. Examples include "Never odd or even."
// and "Don't nod!".
//
// Written 11/2010 by Wayne Pollock, Tampa Florida USA
class Palindrome
{
public static void main ( String [] args )
{
StringBuilder sb = new StringBuilder();
for ( String word : args )
sb.append( word );
// Strip out non text, and convert case:
String text = sb.toString().toUpperCase();
text = text.replaceAll( "[^A-Z0-9]*", "" );
System.out.println( isPalindrome( text ) );
}
// This is a recursive method:
private static boolean isPalindrome ( String text )
{
int len = text.length();
// no or 1 letter text is a palindrome:
if ( len <= 1 )
return true;
// check if first character equals the last:
if ( text.charAt(0) != text.charAt(len-1) )
return false; // not a palindrome!
// If first == last, then check the middle:
return isPalindrome( text.substring(1,len-1) );
}
}