String palindrome in java without using reverse method

Let’s learn string palindrome in java without using reverse method.

String palindrome in java without using reverse method

In java program to check if any string is palindrome without using reverse method first convert user entered string to char array.

In the next step read char from char array from last index. Lastly concatenate. Let’s see java program.

import java.util.Scanner;
public class StringPalindromeDemo 
{
   public static void main(String[] args) 
   {
      Scanner sc = new Scanner(System.in);
      System.out.println("Please enter string to check palindrome: ");
      String strInput = sc.nextLine();
      // converting string to char array
      char[] chString = strInput.toCharArray();       
      // storing reverse string
      String strReverse = "";         
      // reading char by char
      for(int a = chString.length - 1; a >= 0; a--) 
      {
         strReverse = strReverse + chString[a];
      } 
      // printing given string and reversed string
      System.out.println("Given string: " + strInput);
      System.out.println("Reverse String: " + strReverse); 
      // check if given string is palindrome
      if(strInput.equals(strReverse))
      {
         System.out.println("string is palindrome.");
      }
      else
      {
         System.out.println("string is not palindrome.");
      }
   }
}


Output:

Please enter string to check palindrome: madam
Given string: madam
Reverse String: madam
string is palindrome.

Please enter string to check palindrome: abcd
Given string: abcd
Reverse String: dcba
string is not palindrome.


Also read – arrays in java