Palindrome Test

The palindrome tester is another white-board favorite for interviewees. For those that do not know, a palindrome is any sequence of characters and/or numbers that is the same read forwards or backwards. BOB is a palindrome since BOB is BOB backwards. Get it? Got it? Good!

So, the easiest way of checking if a given string is a palindrome, is to reverse it and check it against the original. As usual, there are a whole bunch of ways to solve this. This solution is the easiest I can think of.

public static void main(String[] args) {

  String input = args[0];

  String reversed = "";

  for (int i = input.length()-1; i>=0; i--)
  {
    reversed += input.charAt(i);
  }

  if (input.equals(reversed))
  {
    System.out.println("Palindrome!");
  }
  else
  {
    System.out.println("NON Palindrome!");
  }
}

Leave a Reply

Your email address will not be published. Required fields are marked *