Member-only story
Write a Java program to check whether a string is a Palindrome or not ?
2 min readSep 21, 2024
In this article, we will learn how to check if a string is a palindrome in Java. A string is said to be a palindrome if it is the same if we start reading it from left to right or right to left.
Example of Palindrome:
Input: “noon”
Output: Yes
Input: “moom”
Output: Yes
Input: “name”
Output: No
Input: “madam”
Output: Yes
package com.example.spring_boot_rule_engine_demo;
public class PalindromeTest {
public static void main(String[] args) {
String input = "noon";
System.out.println("is " +input+ " palindrome string : ");
StringBuilder str1 = new StringBuilder(input);
str1.reverse();
if (input.equals(str1.toString())) {
System.out.println("Yes.");
} else {
System.out.println("No.");
}
}
}
Code Explanation:
- The
public static void main(String[] args)
method is the entry point of any Java program. - Input String:
String input = "noon";
initializes a string variable calledinput
with the value…