What is the most efficient algorithm for reversing a String in Java?

reverse a string in java
how to reverse a string in java word by word
how to reverse a string in java without using reverse function
fastest way to reverse a string java
reverse a string in c
how to reverse a string array in java
reverse a string in place java
reverse alternate words in a string java

What is the most efficient way to reverse a string in Java? Should I use some sort of xor operator? The easy way would be to put all the chars in a stack and put them back into a string again but I doubt that's a very efficient way to do it.

And please do not tell me to use some built in function in Java. I am interested in learning how to do it not to use an efficient function but not knowing why it's efficient or how it's built up.

You say you want to know the most efficient way and you don't want to know some standard built-in way of doing this. Then I say to you: RTSL (read the source, luke):

Check out the source code for AbstractStringBuilder#reverse, which gets called by StringBuilder#reverse. I bet it does some stuff that you would not have considered for a robust reverse operation.

Data Structure Questions and Answers – Reverse a , What is the best data structure that can be used to reverse a given string state reasons? Facts and Terminology. As you probably know, Java uses UTF-16 to represent String.The char data type and the Character class are based on the original Unicode specification, which defined

The following does not deal with UTF-16 surrogate pairs.

public static String reverse(String orig)
    char[] s = orig.toCharArray();
    int n = s.length;
    int halfLength = n / 2;
    for (int i=0; i<halfLength; i++)
        char temp = s[i];
        s[i] = s[n-1-i];
        s[n-1-i] = temp;
    return new String(s);

Reverse a string in Java, How do you write a reverse string program in Java? I have the following two approaches for writing a custom string reverse algorithm and I was hoping someone could tell me which is the most efficient / preferable option and explain why. I am fairly new to Java and this would be educational for me. FYI I know this can be done using Stringbuilder but I wanted to write my own solution.

You said you don't want to do it the easy way, but for those Googling you should use StringBuilder.reverse:

String reversed = new StringBuilder(s).reverse().toString();

If you need to implement it yourself, then iterate over the characters in reverse order and append them to a StringBuilder. You have to be careful if there are (or can be) surrogate pairs, as these should not be reversed. The method shown above does this for you automatically, which is why you should use it if possible.

How to Reverse a String in place in Java - Example, How to set Precision for Double values in Java? Program to print half Diamond star pattern. Most visited in  Vikz wrote: I have a String of 1 million characters.What is the most speed and memory efficient algorithm to reverse it.?You could ask yourself, why do I have this String and why do I need to reverse it? Maybe the char sequence isn't inherently constant. Then it shouldn't be a String in the first place.

An old post & question, however still did not see answers pertaining to recursion. Recursive method reverse the given string s, without relaying on inbuilt jdk functions

    public static String reverse(String s) {
    if (s.length() <= 1) {
        return s;
    return reverse(s.substring(1)) + s.charAt(0);


algorithm to reverse a string array in O(n/2) complexity, The algorithm to reverse the String in place is similar to the algorithm we questions and Java Programming interview exposed, two of the best  Using built in reverse() method of the StringBuilder class: String class does not have reverse() method, we need to convert the input string to StringBuilder, which is achieved by using the append method of StringBuilder. After that, print out the characters of the reversed string by scanning from the first till the last index.

The fastest way would be to use the reverse() method on the StringBuilder or StringBuffer classes :)

If you want to implement it yourself, you can get the character array, allocate a second character array and move the chars, in pseudo code this would be like:

String reverse(String str) {
    char[] c = str.getCharArray
    char[] r = new char[c.length];
    int    end = c.length - 1

    for (int n = 0; n <= end; n++) {
        r[n] = c[end - n];

    return new String(r);

You could also run half the array length and swap the chars, the checks involved slow things down probably.

The Right Way to Reverse a String in Java, There are many ways of reversing a string but best possible case should be considered keeping space and time complexities in mind. There are various sorting algorithms, and they're not all equally efficient. We'll be analyzing their time complexity in order to compare them and see which ones perform the best. The list of algorithms you'll learn here is by no means exhaustive, but we have compiled some of the most common and most efficient ones to help you get started:

What is the quickest way to reverse a string with over 10000 , Usually, not writing code to solve problems is a good idea — reverse a String using this simpler implementation than more common  I was asked recently what was the most efficient way to reverse an array in Javascript. At the moment, I suggested using a for loop and fiddling with the array but then realized there is a native Array.reverse() method.

How to Reverse a String in place in Java, How do I resolve the Java.lang. The fastest way to reverse a string is not to reverse it at all - instead, iterate The std::reverse() algorithm probably does this. What is the most efficient algorithm to achieve the following: 0010 0000 => 0000 0100. The conversion is from MSB->LSB to LSB->MSB. All bits must be reversed; that is, this is not endianness-swapping.

Reverse a String in Java in 10 different ways, You must write your best code during programming interviews. The code which can Here is the iterative algorithm to reverse String in place: You can also add more unit tests to further test our method of reversing String in Java. Btw, If you  This problem is a subcase of 'reversing words of a string'. There are many ways of reversing a string but best possible case should be considered keeping space and time complexities in mind. Complete source code can be downloaded from here. Steps to solve this algorithm take two pointers, one pointing to the start of…

  • These kind of questions have been stoned to death for C/C++. Especially The Art of Computer Programming by D. Knuth goes in to a lot of detail.
  • "stoned to death", hahaha, i like that.
  • Out of curiosity: does anyone know of a real use for this? I mean a place where there's a need for an efficient string reversing algorithm?
  • @joachim sauer, interviews maybe.
  • As an aside: note that reversing the sequence of code-points is not the same as reversing "the string". For example, combining characters: if you just reverse the code-points then they end up combining against what was originally the previous character - so "aĉe" (if written with a combining character) could become "ecâ".
  • By the way... when I say "bet it does some stuff that you would have considered"... I was talking about surrogate pairs :-). You can see it in the code.
  • +1 for link to source. One of the best ways to learn how to implement something is to see how it is done in the real world.
  • +1 for directly answering the question, being one of the few that's not on a tangent
  • Link seems broken
  • Here's a link that works, but no easy way to link directly to that method: kickjava.com/src/java/lang/AbstractStringBuilder.java.htm
  • haha I coded the EXACT same thing before searching online :) +1