Write a java program to checks whether a given string is a palindrome or not

Python Program to Check a String an...
Python Program to Check a String and Number is Palindrome Or Not

Java program to checks whether a given string is a palindrome or not

In this program, we learn to chacks whether a given string is a palindrome or not.

Algorithm: Palindrome or not in Java

  1. import java.util.*;
  2. Create: class Palind{
  3. Create main function: public static void main(String args[]){
  4. String original, reverse=””;
  5. Scanner in = new Scanner(System.in);
  6. Print message “Enter a string to check if it is a palindrome”
  7. Read string: original = in.nextLine();
  8. Find length: int length = original.length();
  9. for ( int i = length -1 ; i >= 0 ;i– )
    1. reverse = reverse + original.charAt(i);
  10. if (original.equals(reverse)) {

  11. System.out.println(“Entered string is a palindrome.”);

  12. else: System.out.println(“Entered string is not a palindrome.”);

Source Code: Palindrome or not

import java.util.*;

class Palind
{
public static void main(String args[])
{
String original,reverse= "";
Scanner in=new Scanner(System.in);

System.out.println("Enter a string to check if it is a palindrome");
original =in.nextLine();

int length=original.length();

for (int i=length -1; i>= 0; i--)
reverse =reverse +original.charAt(i);

if (original.equals(reverse))
System.out.println("Entered string is a palindrome.");
else
System.out.println("Entered string is not a palindrome.");

}
}

Expected Output:

 
Enter a string to check if it is a palindrome
Madam
Entered string is a palindrome
 
Write a java program to Checks whether a given string is a palindrome or not

 

Recommended Post:

Get Salesforce Answers

Hi, I’m Pramod Kumar Yadav & a computer science Engineer with 3years of experience in Digital marketing. I’m self-motivated and love to do SEO.

Leave a Reply

%d bloggers like this: