Program to check whether the given string is palindrome or not

Palindrome is a string, which when read in both forward and backward way is the same. For example, "radar" is palindrome but "abc" is not palindrome.


PROGRAM
import java.util.Scanner;

class CheckStringForPalindrome {

 public static void main(String args[]) {
  
  Scanner s=new Scanner(System.in);
  System.out.print("Enter the String : ");
  
  String originalStr=s.nextLine();
  String reverseStr="";
  
  for(int i=originalStr.length()-1;i>=0;i--)
  {
   reverseStr = reverseStr + originalStr.charAt(i);
  }
  
  if(reverseStr.equalsIgnoreCase(originalStr) == true)
   System.out.println("String is Palindrome.");
  else
   System.out.println("String is not Palindrome.");
 }
}
OUTPUT 1
C:\>javac CheckStringForPalindrome.java
C:\>java CheckStringForPalindrome.java
Enter the String : Hello
String is not Palindrome.
OUTPUT 2
C:\>javac CheckStringForPalindrome.java
C:\>java CheckStringForPalindrome.java
Enter the String : radar
String is Palindrome.

Popular posts from this blog

Program to define a class 'employee' with data members as empid, name and salary. Accept data for 5 objects using Array of objects and print it.

Define a class Student with four data members such as name, roll no.,sub1, and sub2. Define appropriate methods to initialize and display the values of data members. Also calculate total marks and percentage scored by student.

Program to input age from user and throw user-defined exception if entered age is negative