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.

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


  • 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