Java program to check String is a palindrome or not
Check whether a String is Palidrome
In this article we will see if the string is palindrome or not in java programming language.
A string is palindrome if the reverse and the original string is same
Lets understand this with the help of an example:-
- Input sting:- AMA
- Reverse sting:- AMA
Here AMA = AMA so this is a palindrome

Algorithm
- Take a String input from user and store it in a variable called “s”
- Take a variable called as “rev” to store reverse value
- Take a i loop and start it from i=s.length()-1 to i>=0
- Then get each character one by one using charAt() and store it in “rev” variable
- If rev variable is equals to s variable then print String is palindrome else print String is not palindrome

Code in Java
Run
import java.util.Scanner; public class StringIsAPalindromeOrNot { public static void main(String[] args) { String s = "arora"; String rev = ""; for (int i = s.length()-1; i >=0 ; i--) rev=rev+s.charAt(i); if(s.equals(rev)) System.out.println("String is palindrome"); else System.out.println("String is not palindrome"); } }
Output
String is palindrome
Note
In this java program, we’re going to check string input by the user will be palindrome or not. Take a String input from the user and store it in a variable called “s”. A palindrome string is one that will be the same if we read from forward and from backward also. Strings like ‘madam’, ‘lol’, ‘pop’, ‘arora’ are examples of palindrome string . We’re storing a reversed string in ‘rev’ variable after that we will compare that original string and the reversed string are same or not using equals() method.
Method 2 (Check whether a String is Palidrome)
Here the reverse of the string will be stored and then will be checking if the original and the reverse is same or not.
Run
import java.util.*; public class Main { public static void main(String args[]) { String original = "Prepinsta", reverse = ""; // Objects of String class int length = original.length(); for (int i = length - 1; i >= 0; i--) reverse = reverse + original.charAt(i); if (original.equals(reverse)) System.out.println("Entered string is a palindrome."); else System.out.println("Entered string isn't a palindrome."); } }
Output
Entered string isn't a palindrome.
Prime Course Trailer
Related Banners
Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription
Login/Signup to comment
import java.util.*;
public class Main
{
public static void main (String args[])
{
Scanner sc = new Scanner (System.in);
System.out.println(“Enter a Word:”);
String word = sc.nextLine();
String rev = “”;
int length = word.length();
for(int i=length-1;i>=0;–i)
{
rev = rev + word.charAt(i);
}
if(word.toLowerCase().equals(rev.toLowerCase()))
System.out.println(“Palindrome”);
else
System.out.println(“Not Palindrome”);
}
}
import java.util.*;
public class Main{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
String s=sc.next();
String temp=””;
StringBuilder sb=new StringBuilder();
for(int i=0;i<s.length();i++)
{
sb.append(s.charAt(i));
}
temp=sb.reverse().toString();
if(temp.equals(s))
System.out.println("Palindrome");
else
System.out.println("not a palindrome");
}
}
import java.util.*;
public class Main{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
String s=sc.next();
String temp=””;
StringBuilder sb=new StringBuilder();
for(int i=0;i<s.length();i++)
{
sb.append(s.charAt(i));
}
temp=sb.reverse().toString();
if(temp.equalsIgnoreCase(s))
System.out.println("Palindrome");
else
System.out.println("not a palindrome");
}
}
public class pallindrome {
public static void main(String[] args) {
String str=”abccba”;
boolean ans=pallindrome(str);
System.out.println(ans);
}
static boolean pallindrome(String str){
for(int i=0;i<str.length()/2;i++){
char start=str.charAt(i);
char end=str.charAt(str.length()-i-1);
if(start!=end){
return false;
}
}
return true;
}
}