Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Java Program to check a String is palindrome or not by ignoring its case


  • A palindrome is a word that reads the same backward or forward.
  • Lets see what if the characters in a palindrome string are in different case. i.e should not be case sensitive. 
  • So now we will see how to check a string is palindrome or not by ignoring its case.
  • Write a function which checks  that string is palindrome or not by converting the original string to lowercase or uppercase.
  • Method should return true if it is palindrome, return false if not a palindrome.


#1 : Java Program to check given string is palindrome or not by ignoring its case.

  1. package com.instanceofjava;

  2. public class CheckPalindrome {
  3. public static boolean isPalindrome(String str) {
  4. StringBuffer strone=new StringBuffer(str.toLowerCase());
  5. StringBuffer strtwo=new StringBuffer(strone);
  6.  
  7.   strone.reverse();
  8.  
  9.   System.out.println("Orginal String ="+strtwo);
  10.   System.out.println("After Reverse ="+strone);
  11.  
  12. if(String.valueOf(strone).compareTo(String.valueOf(strtwo))==0)
  13. return true;
  14.     else
  15.     return false;
  16. }
  17. public static void main(String[] args) {
  18. boolean ispalindrome= isPalindrome("DeleveleD");
  19. System.out.println(ispalindrome);
  20.  
  21.     }

  22. }

Output

  1. Orginal String =deleveled
  2. After Reverse =deleveled
  3. true



This post first appeared on Java Tutorial - InstanceOfJava, please read the originial post: here

Share the post

Java Program to check a String is palindrome or not by ignoring its case

×

Subscribe to Java Tutorial - Instanceofjava

Get updates delivered right to your inbox!

Thank you for your subscription

×