I am a software developer at MindStick soft. Pvt. ltd. I joined this company in august 2021 after completing my post-graduation course. I love to see the historical places of my country.
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);
}
}
Given below is a code to check whether a given string is palindrome or not.
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.
Liked By
Write Answer
Write a java program to check whether a string is a palindrome.
Join MindStick Community
You have need login or register for voting of answers or question.
Aryan Kumar
19-Apr-2023Chnaged Data
Gulshan Negi
07-Apr-2023Hello this is Gulshan Negi
Well, here is the program that you should try.
I hope it will help you.
Thanks
Krishnapriya Rajeev
23-Mar-2023Given below is a code to check whether a given string is palindrome or not.
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.