String Palindrome in Java

Here you will get program for string palindrome in java.

 

A string is palindrome if the string is equal to its reverse. Take below examples.
Palindrome: asdsa, madam, mom, etc.
Not Palindrome: roar, fish, gate, etc.

Below I have shared a program that will check a string is palindrome in java or not. In this program I am reversing the string and then comparing the reversed string with original string. I have used Scanner class to read the string from keyboard. If you find any difficulty to understand the program then you can ask your queries in the comment section.

 

String Palindrome in Java

import java.util.Scanner;

class StringPalindrome
{
 public static void main(String...s)
 {
  String str1="",str2="";
  Scanner sc=new Scanner(System.in);
  System.out.println("Enter a string:");
  
  str1=sc.nextLine();
  
  for(int i=str1.length()-1;i>=0;--i)
   str2=str2+str1.charAt(i);

  if(str1.equals(str2))
   System.out.println("String is Palindrome");
  else
   System.out.println("String is Not Palindrome");
 }
}

 

Output
String Palindrome Program in Java
If you found any mistake in above program for string palindrome in java then please mention it by commenting below.

Leave a Comment

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