---
title: "Write a java program to check whether a string is a palindrome."  
description: "Write a java program to check whether a string is a palindrome."  
author: "Ashutosh Patel"  
published: 2023-03-22  
updated: 2023-04-19  
canonical: https://www.mindstick.com/forum/157543/write-a-java-program-to-check-whether-a-string-is-a-palindrome  
category: "java"  
tags: ["java", "programs"]  
reading_time: 2 minutes  

---

# Write a java program to check whether a string is a palindrome.

Write a [java](https://www.mindstick.com/articles/12214/web-development-company-in-india-laid-on-the-foundation-of-concrete-java-programming) [program to check](https://www.mindstick.com/forum/157542/write-a-java-program-to-check-if-a-list-of-integers-contains-only-odd-numbers) whether a [string](https://www.mindstick.com/articles/1527/string-split-in-c-sharp) is a palindrome.

## Replies

### Reply by Aryan Kumar

Chnaged Data

### Reply by Gulshan Negi

Hello this is Gulshan Negi

Well, here is the [program](https://www.mindstick.com/blog/12337/scaling-up-your-mentorship-program) that you should try.

```plaintext
import java.util.Scanner;
public class PalindromeChecker {
   public static void main(String[] args) {
       Scanner input = new Scanner(System.in);
       System.out.print("Enter a string: ");
       String str = input.nextLine();
       if(isPalindrome(str)) {
           System.out.println(str + " is a palindrome");
       } else {
           System.out.println(str + " is not a palindrome");
       }
   }
   public static boolean isPalindrome(String str) {
       String reverseStr = new StringBuilder(str).reverse().toString();
       return str.equalsIgnoreCase(reverseStr);
   }
}
```

\
I hope it will help you.

Thanks

### Reply by Krishnapriya Rajeev

Given below is a code to [check whether](https://www.mindstick.com/forum/33508/how-to-check-whether-an-android-application-running-in-background) a given string is palindrome or not.

```plaintext
class palindrome
{
    public static void main(String args[])
    {
        String word = "Malayalam";

        int flag=1;
        int n = word.length();
        String rev = "";
        for(int i = n-1; i >= 0; i--)
        {
        	//reversing the string
        	rev += word.charAt(i);
        }

        for(int i = 0; i < n; i++)
        {
            if(rev.charAt(i) != word.charAt(i))
            {
                //checking if reversed string and given string are equal
                if(rev.charAt(i) - word.charAt(i) == 32 || word.charAt(i) - rev.charAt(i)== 32)
                {
                    flag=1;
                }
                else
                {
                	flag=0;
                	break;
                }
            }
        }

        if(flag==1)
        {
            System.out.println(word+" is a palindrome.");
        }
         else
            System.out.println(word+" is not a palindrome.");
    }
}
OUTPUT:
Malayalam is a palindrome.
```

Here, we reverse the given string and store it in another string object. Then, we check each letter of both the given string and reversed string. If the characters are not the same at any point, we set flag = 0 and breaks from the loop.


---

Original Source: https://www.mindstick.com/forum/157543/write-a-java-program-to-check-whether-a-string-is-a-palindrome

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
