Friday, 25 November 2016

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.

No comments:

Post a Comment