Search This Blog


Saturday, December 8, 2012

How to reverse String in Java with or without StringBuffer Example

Reverse String in Java
There are many ways to reverse String in Java. You can use rich Java API to quickly reverse contents of any String object. Java library provides StringBuffer and StringBuilder class with reverse() method which can be used to reverse String in Java. Since converting between String and StringBuffer or StringBuilder is very easy it's the most easy way available to reverse String in Java. At the same time Writing Java program to reverse String in Java without StringBuffer is one of the popular Java String interview question, which requires you to reverse String by applying logic and by not using API methods. Since reverse is a recursive job, you can use recursion as well as loop to reverse String in Java. In this Java tutorial I have shown How to reverse String using StringBuffer, StringBuilder and using pure loop with logic. You can also check How to reverse String with recursion in Java, if you want to see recursive code. let's see complete Java program for this beautiful Java programming exercise.

Java program to reverse String in Java

Java program to reverse String in Java without StringBuffer or StringBuilderHere is my complete code program to reverse any String in Java. In main method we have first used StringBuffer and StringBuilder to reverse contents of String and then we wrote our own logic to reverse String. This uses toCharArray() method of String class which return character array of String. By looping through character array and appending it into empty String we can get reversed String in Java, as shown in following example.

 * Java program to reverse String in Java. There are multiple ways to reverse
 * String in Java, you can either take help of standard Java API StringBuffer
 * to reverse String in Java. StringBuffer has a reverse() method which return StringBuffer
 * with reversed contents. On the other hand you can also reverse it by applying your
 * own logic, if asked to reverse String without using StringBuffer in Java. By the way
 * you can also use StringBuilder to reverse String in Java. StringBuilder is non thread-safe
 * version of StringBuffer and provides similar API. You can use StringBuilder's reverse()
 * method to reverse content and then convert it back to String
 * @author

public class StringReverseExample {
    public static void main(String args[]) {
        //quick wasy to reverse String in Java - Use StringBuffer
        String word = "HelloWorld";
        String reverse = new StringBuffer(word).reverse().toString();
        System.out.printf(" original String : %s , reversed String %s  %n", word, reverse);
        //another quick to reverse String in Java - use StringBuilder
        word = "WakeUp";
        reverse = new StringBuilder(word).reverse().toString();
        System.out.printf(" original String : %s , reversed String %s %n", word, reverse);
        //one way to reverse String without using StringBuffer or StringBuilder is writing
        //own utility method
        word = "Band";
        reverse = reverse(word);
        System.out.printf(" original String : %s , reversed String %s %n", word, reverse);
    public static String reverse(String source){
        if(source == null || source.isEmpty()){
            return source;
        String reverse = "";
        for(int i = source.length() -1; i>=0; i--){
            reverse = reverse + source.charAt(i);
        return reverse;

original String : HelloWorld , reversed String dlroWolleH
original String : WakeUp , reversed String pUekaW
original String : Band , reversed String dnaB

That's all on How to reverse String in Java with and without StringBuffer and StringBuilder. Though being a Java programmer I prefer to use library and suggest any one to use StringBuffer or StringBuilder to reverse String for any production use. Though its also a good programming exercise and you should practice it before going for any Java programming interview.

Other Java tutorials you may like


  1. Indeed it's commonly asked to write a Java program to reverse String in Java without using reverse function and it's not EASY. first of all reverse can have different meaning e.g. reverse word by word or reverse each character, preserving whitespace etc. They may ask you to use recursion to write reverse String function instead of using for loop. Best way is to prepare well with all kinds of String related question.

    1. public class ReverseString {

      private static String hello = "Hello World";

      public static void main(String[] args)

      public static String reverseString(String s) {
      char c[] = s.toCharArray();
      int i = 0, j = c.length - 1;
      while (i < j) {
      char tmp = c[i];
      c[i] = c[j];
      c[j] = tmp;
      return new String(c);

  2. Another solution:

    public static String reverse(String s) {

    if(s.length() == 1){
    return s;
    return s.charAt(s.length()-1)+reverse(s.substring(1, s.length()-1))+s.charAt(0);

  3. you can find different ways @

  4. But,the method isEmpty() is undefined for the type String

  5. input= HELLO WORLD
    output= OLLEH DLROW

    how will get this output without using any inbuild function??
    plz provide me solusn

    1. you can split the string in two strings via locating the space, then reverse them and output

    2. String S= "Hello World";
      String[] SP = S.split(" ");

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

    3. Above code will throw java.lang.StringIndexOutOfBoundsException
      if the number of characters in the string is even.
      Although it will work perfectly in case of odd number of characters.

  6. why you making people fools you, haven't uses the reverse method at all.......,because if you comment the whole reverse method defined below still you are going to get the

  7. int length=as.length();
    String reverse="";
    for ( int i = length - 1 ; i >= 0 ; i-- )
    reverse = reverse + as.charAt(i);

    1. **Cannot invoke setText(String) on the primitive type int

      what should I do?

  8. public class REVERSE {
    String abc = "Hello World i should be reversed";
    void reverse(){
    char[] rev =abc.toCharArray();
    for(int i =abc.length()-1 ;i>=0;i--){
    void wordrevese() {
    String [] Tok = abc.split(" ");

    for(int k = Tok.length-1 ;k>=0;k--){
    System.out.print(Tok[k]+" ");


    public static void main(String[] args){


  9. Using reverse very easy for us...

    public static void main(String[] args) {
    System.out.println("Enter the String...");
    Scanner scan=new Scanner(;
    String x =;
    StringBuffer sb=new StringBuffer(x);


  10. Another Program:

    public class ReverseString {
    public String reverseString(String str) {

    if (str.length() == 1) {
    return str;
    String reverse="";

    reverse += str.charAt(str.length() - 1)
    + reverseString(str.substring(0, str.length() - 1));
    return reverse;
    public static void main(String a[]) {
    ReverseString srr = new ReverseString();
    System.out.println("Result: " + srr.reverseString("Java Programming"));


  11. how about
    input:dear god
    output:god dear

  12. why we write like this String reverse="";why it shows error i can write String reverse;