Categories: Frequently Asked Java Programs In Interview

Frequently Asked Java Program 02: Java Program to Check if Any String is Palindrome Without Using Reverse Method

Hello Folks,

As part of Frequently Asked Java Programs In Interviews For Freshers And Experienced,  in this post we will see a Java program to verify if a given string is palindrome without using inbuilt reverse method.

WHAT IS A PALINDROME STRING?

A palindromic string is a string that remains the same when its characters are reversed.

For Example: NAAN, AMMA AMMA etc.

Logic:

Since we can not use built in reverse method, so we will use different logic.

  1. We will convert given string in to char array first.
  2. Read char from char array from last index and concatenate.

Java Program:

package StringPrograms;

import java.util.Scanner;

public class PalindromeCharString {
        public static void main(String[] args) {

                // Reading string from user input
                Scanner sc = new Scanner(System.in);
                System.out.println("Please enter the string to check palindrome:");
                String inputByUser = sc.nextLine();
                
                // Closing input scanner 
                sc.close();
                
                // converting string in to char array
                char[] stringInChar = inputByUser.toCharArray();
                
        // Declaring an empty string variable to store reverse string
        String reverseInput = "";
                
        // reading char by char from end of array till first and forming a string
                for (int i = stringInChar.length - 1; i <= 0; i--) {
                        reverseInput = reverseInput + stringInChar[i];
                }

        // Printing both actual and reversed string
                System.out.println("Inout string:" + inputByUser);
                System.out.println("Reverse String:" + reverseInput);

        // Checking if given string is plaindrome
                if (inputByUser.equals(reverseInput))
                        System.out.println("Entered string is palindrome.");
                else
                        System.out.println("Entered string is not palindrome.");
        }
}

 

Output:
Please enter the string to check palindrome:
NANA
Inout string:NANA
Reverse String:ANAN
Entered string is not palindrome.
===============================================================
Please enter the string to check palindrome:
AMMA
Inout string:AMMA
Reverse String:AMMA
Entered string is palindrome.

You can run above program for multiple inputs and if it fails for any condition, let me know.

#HappyCoding

Author: Amod Mahajan

My name is Amod Mahajan and I am an IT employee with 4+ years of experience in Software testing and staying in Bengaluru. My area of interest is Automation testing. I started from basics and went throw so many selenium tutorials. Thanks to Mukesh Otwani as his tutorials are easy and cover basics to advance. I have habit of exploring concepts by deep diving. I used to make notes. I thought of sharing my knowledge through posts and now I am here. #KeepLearning #ShareLearning

Amod Mahajan

My name is Amod Mahajan and I am an IT employee with 4+ years of experience in Software testing and staying in Bengaluru. My area of interest is Automation testing. I started from basics and went throw so many selenium tutorials. Thanks to Mukesh Otwani as his tutorials are easy and cover basics to advance. I have habit of exploring concepts by deep diving. I used to make notes. I thought of sharing my knowledge through posts and now I am here. #KeepLearning #ShareLearning

Recent Posts

How Much Java Required For Selenium?

"How much Java I need to learn for selenium with Java binding?" is mostly asked question by a Professional who…

9 hours ago

How To Verify If An Input Box Accepts Only Numbers Through Selenium

Hello Guys, You should not be able to type alphabets or special characters in a field which supposed to accept…

11 hours ago

How To Verify Maximum Character Limit of an Input Box Through Selenium

Hello Folks, Recently a guy asked me this question which he was asked in an interview in IBM. What the…

24 hours ago

API Testing Tutorial Part 15 – Sending GET Request With Params in Postman

Hello Folks, As part of our API Testing series, we will see “Sending GET request with params in Postman”. In last…

5 days ago

TestNG Tutorials 62: Dependency in TestNG – Types of Dependencies in TestNG

We have learnt in previous posts regarding establishing relationship between test methods. You can go through them below: Dependency in…

1 week ago

TestNG Tutorials 61: Dependency in TestNG – Creating Dependency Among Test Methods – DependsOnGroup

In previous post, We have learnt to Establish dependency among test methods. In this post, we will see another concept…

1 week ago