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.


import java.util.Scanner;

public class String_buffer
public static void main(String[] args)
 Scanner sc = new Scanner(System.in);
 System.out.print("Enter string which u want to check : ");
 String str=sc.nextLine(); 
StringBuffer s1=new StringBuffer(str);
StringBuffer s2=new StringBuffer(s1);


System.out.println("Given String is : "+s2);

System.out.println("Reverse String is : "+s1);

System.out.println(s2 + " is Palindrome");
System.out.println(s2 + " is Not Palindrome");



Enter string which u want to check : madam
Given String is : madam
Reverse String is : madam
madam is Palindrome
5 1 vote
Article Rating
Category: Java Programming String Programs Tags:

About Tunde A

My name is Tunde Ajetomobi, a Tech Enthusiast and Growth Hacker. I enjoy creating helpful content that solves problem across different topics. Codezclub is my way of helping young aspiring programmers and students to hone their skills and find solutions on fundamental programming languages.

Notify of

Newest Most Voted
Inline Feedbacks
View all comments

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

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