JAVA PROGRAM TO CHECK A STRING IS PALINDROME OR NOT

 

 

 

 

Palindrome  String    is   a   sequence  of  characters  that   reads  the  same  forwards  and   backwards  i.e.  the  reverse  of  a  palindrome  string  is  identical  to  the  original  String.

 

 

 

Example:

 

 String   s1= ” level”

 Reverse  of  the  String  s1=”level”

 

Output:  String  s1  is  a  palindrome  string

 

 

String  s2=”flower”;

Reverse of  string   s2=”rewolf”;

Output: String  s2  is  not  a  palindrome  String

 

 

 

      

 

Approach:

 

First  convert  all  the  characters  in  the  string  to  Lowercase. After  that reverse  the  given  original  String.  After  reversing  if  the  reversed  string  is  same  as  original  string,  then  the  given  string  is  a  palindrome  string.

 

 

 

 import   java.util.*;

Public  class   Palindrome{

public  static  void  main( String   args[] ) {

    Scanner  sc=new  Scanner(System.in);

     System.out.println(“Enter  a  String  to  check  whether  a  given  string  is  palindrome  or  not”);

      String  str=sc.nextLine();

       str=str.toLowerCase();

       String   reverse=” “;

       int  length=str.length();

       for(int  i=length-1;i>=0;i– ) {

           reverse=reverse+str.charAt(i);

     }

     System.out.println(“Reverse  of  the  string  is : “+reverse);

 

    if(str.equals(reverse)){

      System.out.println(“Given  String  is  Palindrome”);

     }

    else{

         System.out.println(“Given  string  is  not  a  palindrome  string”);

          }

 

   }

}

 

      

 

 

 

 

Leave a Comment

Your email address will not be published. Required fields are marked *