-->

Hello,This is me!

ITEngineer8995

Professional Coder Coding is my passion

Wednesday, July 1, 2020

Java Program To Check Palindrome Using While Loop with charAt() Function

  • July 01, 2020
  • by

Java Program To Check Palindrome Using While Loop with charAt() Function

In this Java Program, we check palindrome using for loop with charAt() function.

A word, phrase, number or sequence of words that reads same backward as forward are known as palindromes.

E.g. madam, civic, level, refer, 121 1331 etc.

Logic :   while(i >= 0)
            {
                reverseString = reverseString + inputString.charAt(i);
                i--;
            }

Step 1 : Import java.util.Scanner

Step 2 : Create variable reverseString

Step 3 : Ask the user to enter the string

Step 4 : Create variable inputString and store the user entered input into it.

Step 5 : Create variable length and store the length of the user entered input string into it.

Step 6 : Create variable i and store the length-1 output in it.

Step 7 : Apply while loop

Step 8 : Apply if else and display the result.

Program :

import java.util.Scanner;

public class PalindromeWhile 
    {
        public static void main(String[] args)
        {
            String reverseString="";
            
            Scanner sc = new Scanner(System.in);
            
            System.out.println("Enter the String:");
            String inputString = sc.nextLine();
            
            int length = inputString.length();
            
            int i = length-1;
            while(i >= 0)
            {
                reverseString = reverseString + inputString.charAt(i);
                i--;
            }
            
            if(inputString.equals(reverseString))
                System.out.println("Input String is a Palindrome");
            else
                System.out.println("Input String is not a Palindrome");
        }
    
}

Output :

Enter any String : abccba
abccba
The input String is a Palindrome.

Enter any String : abc
abc
The input String is not a Palindrome.

For execution of the same do visit my YouTube Channel :




A blog full of codes of various languages like Java, .net, C, C++ etc with their outputs and some other IT stuff.

0 comments:

Post a Comment

ITEngineer8995
India

SEND ME A MESSAGE

Search This Blog

Powered by Blogger.

Pages