Write a Java Program to Check String is palindrome or not using String Buffer

By | 12.03.2017

Java Program to Check String is palindrome or not using String Buffer


Write a Java Program to Check String is palindrome or not using String Buffer.In this program, it is very easy to check the given string is Palindrome or not.

In this program ,First convert the String to StringBuffer. I am reversing the string using reverse() method in String Buffer Class and then comparing the reversed string with original string using compareTo() method in string.


Steps: :


  • Get the input String
  • Find reverse of the given string
  • Match the original string with reverse string, If both are equal, then given string is Palindrome, Otherwise Not.

Procedure : :


  • A string str is taken as an input to the program.I have used Scanner class to read the string from keyboard.
  • Create a String Buffer object s1 for the str because the reverse() method is in String Buffer.
  • Then, create a String Buffer object s2 for the s1.
  • Call the reverse method i.e s1.reverse() reverse the string associated with StringBuffer object.
  • Call the compareTo() method to compare input string and the resultant reverse string for checking the given string is palindrome or not.

 

Palindrome: :

  • radar, madam, mom, dad , etc.

Not Palindrome: :

  • Codezclub, java, road, car,  etc.

The following code accepts a string, and checks whether the given string is Palindrome or not using String Buffer.The Program below is successfully compiled and run on the Windows System to produce desired output.


SOURCE CODE : :



OUTPUT : :


2
Leave a Reply

avatar
2 Comment threads
0 Thread replies
0 Followers
 
Most reacted comment
Hottest comment thread
0 Comment authors
Codez Club  Post authorAyusha Recent comment authors
  Subscribe  
newest oldest most voted
Notify of
Ayusha
Guest
Ayusha

I wrote a program for the palindrome without declaring a string in the beginning. I took both the strings as StringBuffer and reversed one of them using the reverse method. Why am I still not getting the right answer?

Codez Club  Post author
Guest
Codez Club Post author

Can u please post your code here, then we can see what to next ?