Java program to check whether string is palindrome using library methods

Let’s learn java program to check whether string is palindrome using library methods.

Java program to check whether string is palindrome using library methods

Java program to check whether string is palindrome using library methods

Palindrome string program in java is the common java interview question.

Also read – string palindrome in java using reverse method

Palindrome string is a string which is same even if it is reversed. For example,

eye, malayalam, abcba etc.,

Here’s the java program to check whether string is palindrome using library methods,

public class StringPalindromeJava
{
   public static void isPalindrome(String str)
   {
      String strReverse = new StringBuffer(str).reverse().toString();
       // checking for palindrome
      if(str.equals(strReverse))
      {
         System.out.println(str + " is palindrome string.");
      }
      else
      {
         System.out.println(str + " is not palindrome string.");
      }
   }

   public static void main(String[] args)
   {
      // palindrome java
      isPalindrome("eye");
      isPalindrome("rotator");
   }
}



Output:

eye is palindrome string.
flowerbrackets is not palindrome string.


Also read – multidimensional array in java

Also read – string palindrome in java without using reverse method


String palindrome in java using array

Let’s learn to check whether given string is palindrome using array. To do that first convert user entered string into character array using toCharArray() method.

In the next step make a copy of user entered string into another character array using copyOf() method of Arrays class.

Also read – string palindrome in java using recursion

Now reverse array. Compare user entered array with reversed array. If both array are same then string is palindrome else string is not palindrome.

Here’s the java program to verify string palindrome in java using array,

import java.util.Arrays;
import java.util.Scanner;

public class StringPalindromeUsingArray 
{
   public static void main(String[] args) 
   {
      System.out.println("Please enter string to check whether string is palindrome: ");
      Scanner sc = new Scanner(System.in);
      String strInput = sc.nextLine();
      char[] chArray = strInput.toCharArray();
      int size = chArray.length;
      char[] chGiven = Arrays.copyOf(chArray, chArray.length);
      for(int a = 0; a < size / 2; a++) 
      {
         char temp = chArray[a];
         chArray[a] = chArray[size - a - 1];
         chArray[size - a - 1] = temp;
      }
      System.out.println("Given array: " + Arrays.toString(chGiven));
      System.out.println("String palindrome in java using array: " + Arrays.toString(chArray));
      if(Arrays.equals(chArray, chGiven)) 
      {
         System.out.println("string is palindrome.");
      }
      else 
      {
         System.out.println("string is not a palindrome.");
      }
      sc.close();
   }
}


Output:

Please enter string to check whether string is palindrome: madam
Given array: [m, a, d, a, m]
String palindrome in java using array: [m, a, d, a, m]
string is palindrome.

Please enter string to check whether string is palindrome: hello
Given array: [h, e, l, l, o]
String palindrome in java using array: [o, l, l, e, h]
string is not a palindrome.


String palindrome in java without using inbuilt function

Here’s the java program to check whether string is palindrome without using inbuilt function,

import java.util.Scanner;

public class WithoutInbuiltFunction 
{
   public static void main(String[] args) 
   {
      Scanner sc = new Scanner(System.in);
      System.out.println("Please enter string to check whether string is palindrome: ");
      String strInput = sc.next();
      String strReverse = "";
      for(int a = strInput.length() - 1; a >= 0; a--) 
      {
         strReverse = strReverse + strInput.charAt(a);
      }
      if(strReverse.equals(strInput)) 
      {
         System.out.println("string is palindrome.");
      }
      else
      {
         System.out.println("string is not palindrome.");
      }
      sc.close();
   }
}


Output:

Please enter string to check whether string is palindrome: madam
string is palindrome.

Please enter string to check whether string is palindrome: world
string is not palindrome.


String palindrome in java using stringbuffer

Let’s learn to check whether given string is palindrome using StringBuffer. First we have to create StringBuffer object ‘sb’.

Then pass user entered string as parameter to the constructor. Now reverse contents of given string using reverse() method of StringBuffer class.

In the next step convert StringBuffer object to String using toString() method of String class.

Finally compare reversed string and user entered string. If both are same string is palindrome else string is not palindrome.

Here’s string palindrome in java using stringbuffer,

import java.util.Scanner;

public class PalindromeUsingStringBuffer 
{
   public static void main(String[] args) 
   {
      Scanner sc = new Scanner(System.in);
      System.out.println("Please enter string to check whether string is palindrome: ");
      String strInput = sc.next();
      StringBuffer sb = new StringBuffer(strInput);
      sb.reverse();
      String str = sb.toString();
      if(strInput.equals(str))
      {
         System.out.println("string is palindrome.");
      }
      else
      {
         System.out.println("string is not palindrome.");
      }
      sc.close();
   }
}


Output:

Please enter string to check whether string is palindrome: malayalam
string is palindrome.

Please enter string to check whether string is palindrome: java
string is not palindrome.