Previously we have written a program to check for palindromes:
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
String x = input.next();
/* ------------------------------------------
Algorithm to check if x is a palindrome
------------------------------------------ */
boolean isPalindrome = true;
int i, last;
last = x.length() - 1;
for ( i = 0; i < x.length()/2; i++ )
{
if ( x.charAt( i ) != x.charAt( last-i ) )
{
isPalindrome = false;
break; // We can exit the loop now
}
}
System.out.println(isPalindrome);
}
|