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 

Check whether a String is Palidrome in java

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
Program to Check given String is Palindrome or Not in java

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

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

11 comments on “Java program to check String is a palindrome or not”


  • Shekhar Suman

    public static void main(String[] args) {
    String str=”racecar”;
    int n=str.length()/2;
    int count=0;
    for (int i=0;i<n;i++){
    if(str.charAt(i)!=str.charAt(str.length()-1-i)){
    System.out.println("Not palindrome");
    count++;
    break;
    }
    }
    if(count==0){
    System.out.println("Palindrome");
    }
    }


  • Khushi Saxena

    import java.util.*;
    class HelloWorld {
    public static void main(String[] args) {
    Scanner sc=new Scanner(System.in);
    String s=sc.nextLine();
    String rev=””;
    for(int i=s.length()-1;i>=0;i–){
    char c=s.charAt(i);
    rev+=c;
    }
    if(s.equals(rev)){
    System.out.print(“Yes”);
    return;
    }
    else{
    System.out.print(“NO”);
    }
    }
    }


  • Vivek

    import java.util.Scanner;
    public class String2 {
    public static void main(String[] args){
    Scanner sc = new Scanner(System.in);
    String s = sc.next();
    StringBuilder rev = new StringBuilder();
    for(int i = s.length()-1; i>=0; i–){
    rev.append(s.charAt(i));
    }
    if(s.equalsIgnoreCase(rev.toString())){
    System.out.println(“Palindrome”);
    }
    else
    System.out.println(“not palindrome”);
    }
    }


  • Palak Gupta

    public static void isPalindrome(String str) {
    boolean sub=true;
    int i=0;
    int j=str.length() – 1;
    while(i < j){
    if(str.charAt(i)!=str.charAt(j)){
    sub=false;
    break;
    }
    i++;
    j–;
    }
    if(sub){
    System.out.println("The entire String is palindrome");
    }
    else{
    System.out.println("The entire String is not palindrome");
    }
    }


  • Vijaykhede

    public class Main {
    public static void main(String[] args){
    System.out.println(“enter a string”);
    Scanner sc=new Scanner(System.in);
    String s= sc.next();
    int i,j;
    for(i=0,j=s.length()-1;ij)
    System.out.println(“palidrome”);
    else
    System.out.println(“not palidrome”);
    }
    }


  • Adrish

    public class Main
    {
    public static void main(String[] args) {
    String str=”aroora”;
    int n=str.length();
    boolean isPalindrome=true;
    for(int i=0;i<n/2;i++){
    if(str.charAt(i)!=str.charAt(n-i-1)){
    isPalindrome=false;
    break;
    }
    }
    if(isPalindrome){
    System.out.println("Palindrome");
    }else{
    System.out.println("Not palindrome");
    }
    }
    }


  • Asif Basheer

    import java.util.*;
    class Baba {
    public static boolean isPalindrome(String s, int n) {
    int l = 0;int r = n-1;
    while(l<=r) {
    if(s.charAt(l) != s.charAt(r)) {
    return false;
    }
    l++;
    r–;
    }
    return true;
    }
    public static void main(String[] agrs) {
    Scanner sc = new Scanner(System.in);
    System.out.print("Enter a String: ");
    String s = sc.next();
    int n = s.length();
    if(isPalindrome(s, n) == true) {
    System.out.println("Palindrome");
    } else {
    System.out.println("Not a Palindrome String");
    }
    }
    }


  • mukul

    package StringProblem;
    import java.util.*;
    public class chekcStringPalindromORnot {
    public static void main(String args[]) {
    Scanner sc = new Scanner(System.in);

    System.out.println(“Enter the string and chekc the string is palindrom or not”);
    String s = sc.next();
    // using for each loop
    String rev = “”;
    for(char c : s.toCharArray()) {
    rev =c+rev;
    }
    if(s.equals(rev))
    System.out.println(“String is palindrom”);
    else
    System.out.println(“not palindrom”);

    }
    }


  • Shubham

    public class JavaApplication1
    {
    public static void main(String[] args)
    {
    Scanner obj = new Scanner(System.in);
    String s = obj.nextLine();
    String c=””;
    for(char i : s.toCharArray())
    {
    c = i + c;
    }
    if(c.equalsIgnoreCase(s))
    System.out.print(“Yes”);
    else
    System.out.print(“NO”);
    }
    }


  • Shobhit

    This can be optimesed as
    j =str1.length()-1;
    for( i=0; i<str1.length(); i++ )
    {
    if( str1.charAt(i) != str1.charAt(j) // i is moving from left to right whereas j from right to left
    return false;

    j–;
    }

    return true ; // for palindrome