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.
Markdown for AI
A clean, structured version of this page for AI assistants and LLMs.
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge that you have read and understood our
Cookie Policy &
Privacy Policy.
Chnaged Data
Hello this is Gulshan Negi
Well, here is the program that you should try.
I hope it will help you.
Thanks
Given 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.