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

Week – 3                        
a)Aim: To Write a Java program that checks whether a given string is a palindrome or not. Ex: MADAM is a Palindrome.
Source Code:
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

Leave a Reply

%d bloggers like this: