Palindrome

A palindrome is a word, phrase, number, or other sequence of units that can be read the same way in either direction.

package org.paingan.labs.logic;

public class Palindrome {
 public static boolean isPalindrome(String word) {
 int left  = 0; // index of leftmost unchecked char
 int right = word.length() -1; // index of the rightmost

while (left < right) { // continue until they reach center
 if (word.charAt(left) != word.charAt(right)) {
 return false; // if chars are different, finished
 }
 left++; // move left index toward the center
 right--; // move right index toward the center
 }

return true; // if finished, all chars were same
 }

public static void main(String args[]) {
 if(isPalindrome("abccba"))
 System.out.println("it's Palindrome!");
 }
 }

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s