Java Program to Check whether a String is a Palindrome

Overview

In this blog, we will develop a Java program that checks whether the given string is a palindrome or not.

What is a Palindrome?

A palindrome string is a string that reads the same backwards as forwards. In other words, if the reverse of a string is same as the original string, then it is called a palindrome. For example, “Radar”, “lol”, etc.

Palindrome Explanation

Program Description

Input a string “str”. The task is to check whether the given string is a palindrome or not. Print “Yes” if it is a palindrome string. Else, print “No”.

Source Code

Here is the source code of the Java program to check whether the given string is a palindrome or not.

Program Explanation

  1. “Scanner” class and its function “next()” is used to obtain the input from the user and “println()” class is used to print the output.
  2. Length of the obtained string is assigned to a variable called “length”.
  3. Take a pointer “i” which initially points to a value of “length-1” i.e. the last character in the string.
  4. Declare another string “reverseString” to hold the value of the reversed string. Now, the ith string character is assigned as the first character to the “reverseString”.
  5. Follow the above step by decrementing “i” till it reaches the first character of the string.
  6. The “reverseString” variable now holds the reversed string.
  7. Compare both the strings “str” and “reverseString” ignoring the case of the letters.
  8. Print “Yes” if the condition is satisfied.
  9. Else, print a “No” as it doesn’t form a palindrome.

How It Works?

Let str = “Guvi”

length of str = 4

reverseString = “ivuG”

Is “guvi” same as “iuvg” (ignoring the letter case)?

Answer: “No”

Test Cases

1.Enter a string: Amma

Yes. It is a palindrome.

2.Enter a string: Java

No. It is not a palindrome.

Are you looking to master coding and crack top company interviews? Go ahead and try GUVI’s Pro Subscription to help you with everything needed to build a strong Technical Career.

Know more about GUVI:

Also, download GUVI’s app to learn the latest IT skills in vernacular languages:

--

--

--

Love podcasts or audiobooks? Learn on the go with our new app.

Technology Responding to New Needs: Hybrid Cloud Technology

MDC’s blend Edition #1: Weekly Roundup — Aug 15

Building a Stock Option Valuation Model with Python: Part II

Data Science Interview Preparation: Programming — Python

Order-Statistics-Based Hash Algorithm

Docker Learning Note (v)

Sourcing Top Software Developers on Github: A 2021 Guide

Sourcing Top Software Developers on Github: A 2021 Guide

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Baruni Priya T S

Baruni Priya T S

More from Medium

Joy of Computer Program's Logic

Java I/O Character Stream Implementation

Diagonal Difference | HackerRank Question | Java Solution

jvm memory tuning