rankvise logo
reverse a string in java

How to Use Reverse A String In Java – A Beginners Guide

It’s time to take on a new Java challenge! In this tutorial, we’ll learn how to reverse a string in Java. You’ll learn four different ways to do this and the pros and cons of each approach.

1. Using String Builder

  • Import java.util.StringBuilder
  • Create StringBuilder object sb = new StringBuilder()
  • Use append method to add “abc”
  • Reverse the string using reverse() method of StringBuilder

You can print or store the reversed string in a variable

public class StringFormatter {  
public static String reverseString(String str){  
    StringBuilder sb=new StringBuilder(str);  
    sb.reverse();  
    return sb.toString();  
}  
}
public class TestStringFormatter {  
public static void main(String[] args) {  
    System.out.println(StringFormatter.reverseString("my name is khan"));  
    System.out.println(StringFormatter.reverseString("I am sonoo jaiswal"));      
    }  
}

Output:

nahk si eman ym
lawsiaj oonos ma I

2. Using String Buffer

You can reverse a string by using StringBuffer class.

  • Create a StringBuffer object:
  • StringBuffer sb = new StringBuffer();
  • Append the string to be reversed to the end of the existing content in your buffer:
  • sb.append(“This is my string for reversing”);
  • Reverse the content of your buffer and get its reversed value back:

For Online Judges problems that does not allow StringBuilder or StringBuffer, you can do it in place using char[] as following

public static String reverse(String input){
    char[] in = input.toCharArray();
    int begin=0;
    int end=in.length-1;
    char temp;
    while(end>begin){
        temp = in[begin];
        in[begin]=in[end];
        in[end] = temp;
        end--;
        begin++;
    }
    return new String(in);
}

3. Using Reverse string

You might already be familiar with Reverse string, which is a method of solving a problem by breaking it down into smaller sub-problems. A Reverse string function calls itself in order to solve the smaller problem.

Reverse string by CHARACTERS:

public static void main(String[] args) {
    // Using traditional approach
    String result="";
    for(int i=string.length()-1; i>=0; i--) {
        result = result + string.charAt(i);
    }
    System.out.println(result);

    // Using StringBuffer class
    StringBuffer buffer = new StringBuffer(string);
    System.out.println(buffer.reverse());    
}

Reverse string by WORDS:

public static void reverseStringByWords(String string) {
    StringBuilder stringBuilder = new StringBuilder();
    String[] words = string.split(" ");

    for (int j = words.length-1; j >= 0; j--) {
        stringBuilder.append(words[j]).append(' ');
    }
    System.out.println("Reverse words: " + stringBuilder);
}

4. Using Inbuilt Reverse Function

public class StringFormatter {  
public static String reverseString(String str){  
    char ch[]=str.toCharArray();  
    String rev="";  
    for(int i=ch.length-1;i>=0;i--){  
        rev+=ch[i];  
    }  
    return rev;  
}  
}
public class TestStringFormatter {  
public static void main(String[] args) {  
    System.out.println(StringFormatter.reverseString("my name is khan"));  
    System.out.println(StringFormatter.reverseString("I am sonoo jaiswal"));      
    }  
}

Output:

nahk si eman ym
lawsiaj oonos ma I

Conclusion

Reverse String in Java is a simple and easy process. With this article, you will learn how to do it using several methods. Each method has its own uniqueness and strengths. So, choose the one that suits your needs best!