Write a java program to checks whether a given string is a 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

Pramod Kumar Yadav is from Janakpur Dham, Nepal. He was born on December 23, 1994, and has one elder brother and two elder sisters. He completed his education at various schools and colleges in Nepal and completed a degree in Computer Science Engineering from MITS in Andhra Pradesh, India. Pramod has worked as the owner of RC Educational Foundation Pvt Ltd, a teacher, and an Educational Consultant, and is currently working as an Engineer and Digital Marketer.



Leave a Comment