Palindrome Java

A palindrome is a word, phrase, number, or other sequence of symbols or elements, whose meaning may

be interpreted the same way in either forward or reverse direction.
Examples: 2222, 3333, aca, ada

Find Palindrome string java without using Java API

public class PalindromeStringCheck {

 /**
  * @param args
  */
 public static void main(String[] args) {
  String word = "aba";
        char[] charArray = word.toCharArray(); 
        System.out.println(istPalindrom(charArray));   

 }

 public static boolean istPalindrom(char[] word){
     int i1 = 0;
     int i2 = word.length - 1;
     while (i2 > i1) {
         if (word[i1] != word[i2]) {
             return false;
         }
         ++i1;
         --i2;
     }
     return true;
 }

}

Palindrome Java: Find Palindrome number in java without using API

public class PalindromeNumberCheck {

 public static void main(String args[]) {
  System.out.println(isPalindrome(2222));

 }

 private static boolean isPalindrome(int number) {
  if (number == reverse(number)) {
   return true;
  }
  return false;
 }

 private static int reverse(int number) {
  int reverse = 0;

  while (number != 0) {
   reverse = reverse * 10 + number % 10;
   number = number / 10;
  }
  return reverse;
 }

}

 Palindrome Java

Leave a Reply

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