Please login


Prepinsta Prime

Video courses for company/skill based Preparation

(Check all courses)
Get Prime Video

Prepinsta Prime

Purchase mock tests for company/skill building

(Check all mocks)
Get Prime mock

Java program to check String is a palindrome or not

Check String is palindrome or not

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.

Java Program to check string is palindrome or not


  • 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

import java.util.Scanner;

public class StringIsAPalindromeOrNot {

	public static void main(String[] args) {
		Scanner sc =new Scanner(;
		 System.out.println("Enter string");
		String s =;
		String rev = "";
		for (int i = s.length()-1; i >=0 ; i--) 
			System.out.println("String is palindrome");
			System.out.println("String is not palindrome");


This code is contributed by Shubham Nigam (Prepinsta Placement Cell Student)


Enter string : arora
String is palindrome

Enter string : prepinsta
String is not palindrome

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

  • Shubham

    public class JavaApplication1
    public static void main(String[] args)
    Scanner obj = new Scanner(;
    String s = obj.nextLine();
    String c=””;
    for(char i : s.toCharArray())
    c = i + c;

  • 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;


    return true ; // for palindrome