Java program to check whether string is palindrome

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

Java program to check whether string is palindrome

Check whether given string is a palindrome or not is one of the common java interview question.

Palindrome string is a string where string remains same when reversed or a word spelled same way forwards and backwards.

Example: level, dad, mom, madam, abcba, etc.,

Also read – armstrong program in java

Here’s the syntax for equalsIgnoreCase() method,

public boolean equalsIgnoreCase(String anotherString)

equalsIgnoreCase() method returns true if the argument is not null and it represents an equivalent String ignoring case; false otherwise.

In the below string palindrome in java user enters a string as input. Then input string is reversed using for loop.

Here equalsIgnoreCase() method is used which compares this string to another string, ignoring case considerations.

Lastly this reversed string is compared with input string. Let’s see java program,

import java.util.*;
  
public class PalindromeStringInJava 
{
   public static void main(String[] args) 
   { 
      String strInput, strReverse = "";
      Scanner sc = new Scanner(System.in);
      System.out.print("Please enter the string : ");
      strInput = sc.nextLine();
      int num = strInput.length();
      for(int a = num - 1; a >= 0; a--)
      {
          strReverse = strReverse + strInput.charAt(a);
      }
      if(strInput.equalsIgnoreCase(strReverse))
      {
         System.out.println("String is palindrome.");
      }
      else
      {
         System.out.println("String is not palindrome.");
      }
      sc.close();
   }
}



Output:

Java program to check whether string is palindrome

Using reverse method

Let’s learn simple string palindrome in java using reverse method. Here’s the palindrome program in java (string) using reverse method,

public class PalindromeReverse 
{
   public static void checkStringPalindrome(String strInput)
   {
      // reversing given string
      String strReverse = new StringBuffer(strInput).reverse().toString();
      // checking whether given string is palindrome or not
      if(strInput.equals(strReverse))
      {
         System.out.println("String is a palindrome");
      }
      else
      {
         System.out.println("String is not a palindrome");
      }
   }

   public static void main(String[] args) 
   {
      checkStringPalindrome("nitin");
   }
}



Output:

String is a palindrome

Without using reverse method

Similarly it is possible to execute palindrome (string) program in java without reversing a string.

In the below java program characters at beginning and at end move towards middle of the string.

Here’s the string palindrome in java without using reverse method,

import java.util.Scanner;
  
public class PalindromeStringDemo 
{
   public static void main(String[] args) 
   {
      Scanner sc = new Scanner(System.in);
      System.out.println("Please enter a string : ");
      String str = sc.nextLine();
  
      int length = str.length();
      int a, start, end, middle;
  
      start = 0;
      end = length - 1;
      middle = (start + end) / 2;
  
      for(a = start; a <= middle; a++) 
      {
         if(str.charAt(start) == str.charAt(end)) 
         {
            start++;
            end--;
         }
         else
         {
            break;
         }
      }
      if(a == middle + 1) 
      {
         System.out.println(str + " - is a palindrome string.");
      }
      else
      {
         System.out.println(str + " - not a palindrome string.");
      }
      sc.close();
   }
}



 

Output:

Please enter a string : level
level – is a palindrome string.

Please enter a string : abcd
abcd – not a palindrome string.