Member-only story

Write a Java program to check whether a string is a Palindrome or not ?

Gain Java Knowledge
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 called input with the value…

--

--

Gain Java Knowledge
Gain Java Knowledge

Written by Gain Java Knowledge

The Java programming language is one of the most popular languages today. Stay up to date with news, certifications, free learning resources and much more.

No responses yet