-->

Hello,This is me!

ITEngineer8995

Professional Coder Coding is my passion

Sunday, June 28, 2020

Java Program To Check Palindrome Using For Loop with charAt() function

  • June 28, 2020
  • by

Java Program To Check Palindrome Using For 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 : for (int i = length - 1; i >=0; i--)
            
                reverseString = reverseString + inputString.charAt(i);
                
Step 1 : Import java.util.Scanner

Step 2 : Ask the user to enter a string to check if it is a palindrome

Step 3 : Create variable length and store the length of the input string entered by the user.

Step 4 : Create variable reverseString

Step 5 : Apply the if loop.

Step 6 : Display the result.

Program :

import java.util.Scanner;

public class PalindromeForLoop 
    {
        public static void main(String[] args)
        {
            String reverseString="";
            Scanner sc = new Scanner(System.in);
            
            System.out.println("Enter a string to check if it is a palindrome:");
            String inputString = sc.nextLine();
            
            int length = inputString.length();
            
            for (int i = length - 1; i >=0; i--)
            
                reverseString = reverseString + inputString.charAt(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