Java String Reverse Algorithm which is the most effcient approch? 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.

Solution 1: uses two arrays, copying the contents of one into another but in reverse order

//include needed util
import java.util.Scanner;

 /**
 * This class will reverse a string
 * @author Richard
 *
 */

public class ReverseString {
/**
 * This method will take in a string, convert that to two 
 * char arrays, then swap the contents of one for the contents
 * of the other except moving in opposite directions and so
 * will reverse the order of the output string  
 * @param args
 */

public static void main(String[] args) {

    //declare variables
    Scanner input = new Scanner(System.in);
    String inputString = "";
    int length = 0, indexOfArrayToChange = 0, indexOfReferenceArray = 0;

    //get input string
    System.out.print("Enter the string you want to reverse: ");
    inputString = input.nextLine();

    //find length of string, it needs to be reduced by one to prevent an error
    //as index start at 0.
    length = inputString.length()-1;
    //set 'indexOfReferenceArray' equal to string.length-1 for use later in loop
    indexOfReferenceArray = length;

    //convert to arrays
    char[] arrayToChange = inputString.toCharArray();
    char[] referenceArray = inputString.toCharArray();

    //loop through and reverse order as long as we are still within bounds of original string length
    while (indexOfArrayToChange&lt;=length) {
        //change the first letter of one array to be the same as the last letter of reference array
        arrayToChange[indexOfArrayToChange] = referenceArray[indexOfReferenceArray];
        //increment/decrement to move through arrays
        indexOfArrayToChange++;
        indexOfReferenceArray--;

    }
    //convert back to string
    String newString = new String(arrayToChange);

    //output result
    System.out.println(newString);

    //close resources
    input.close();
}
}


Solution 2: uses one array and swaps from back to front meeting in the middle. However the code within the loop has to be longer to do the swapping.

//include needed util
import java.util.Scanner;

/**
* This class will reverse an inputed string
* @author Richard
*
*/
public class ReverseString {

/**
 * This method will take in a string, convert that to a 
 * char array, then swap the contents of the array from back to front
 * meeting in the middle.  
 * @param args
 */
public static void main(String[] args) {
    //declare variables
    Scanner input = new Scanner(System.in);
    String inputString = "";
    int length = 0, indexStart = 0, indexEnd = 0;

    //get input string
    System.out.print("Enter the string you want to reverse: ");
    inputString = input.nextLine();

    //find length of string, it needs to be reduced by one to prevent an error
    //as index start at 0.
    length = inputString.length()-1;
    //set 'indexOfReferenceArray' equal to string.length-1 for use later in loop
    indexEnd = length;

    //convert to arrays
    char[] array = inputString.toCharArray();

    //loop through and reverse order until two vars meet in middle of array
    while (indexStart&lt;=(length/2)) {
        //swap array elements
        char temp = array[indexStart];
        array[indexStart] = array[indexEnd];
        array[indexEnd] = temp;
        //increment/decrement to move through arrays
        indexStart++;
        indexEnd--;
    }

    //convert back to string
    String newString = new String(array);

    //output result
    System.out.println(newString);

    //close resources
    input.close();

}

}
Solution 2 is (probably, that depends on many factors) the more efficient one, as its (slightly more complex) loop body is executed only length/2 times, and it doesn't create two character arrays from the input string.

But besides answering your question, I'd like to comment on your coding style - we're on codereview, anyway.

First, I'd always separate input/output and computation, so you can re-use the computation as a "library function" with different input/output or as part of a bigger computation.

Then, you often declare a variable before you want to use it (and even assign an unnecessary initial value to it). It's accepted style to declare a variable exactly when you need it first, and assign it the value you want.

You "adjusted" the length value by -1 because you need that value for the endIndex. That's misleading, as now you have a variable named length, but it doesn't contain the length value. This adjustment should be done in the endIndex variable, not the length.

It's good that you are using Javadoc comments. But you should document pieces of code from an "outside" view: what the method does, not how it's doing its job. See my re-worded javadoc for the main() method.

As a matter of personal style, I'd replace your while loop with a for loop that uses two variables: in every iteration, it increases indexStart and decreases indexEnd.

See my modified version of your solution 2:

package test;

import java.util.Scanner;

public class ReverseString {

    /**
     * This method will read a String from standard input 
     * and print the character-reversed string to standard output.  
     * @param args
     */
    public static void main(String[] args) {
        //declare variables
        Scanner input = new Scanner(System.in);
        // Don't introduce variables before you need them
        // String inputString = "";
        // int length = 0, indexStart = 0, indexEnd = 0;

        //get input string
        System.out.print("Enter the string you want to reverse: ");
        String inputString = input.nextLine();

        String newString = reverseString(inputString);

        //output result
        System.out.println(newString);

        //close resources
        input.close();
    }

    /**
     * Returns the character-reversed String of the inputString
     * @param inputString the input String
     * @return the input string in reversed order of the characters
     */
    public static String reverseString(String inputString) {
        //find length of string, it needs to be reduced by one to prevent an error
        //as index start at 0.
        // NO! It's not the length that's wrong, it's the indexEnd
        int length = inputString.length();
        //set 'indexOfReferenceArray' equal to string.length-1 for use later in loop
        // int indexEnd = length;

        //convert to arrays
        char[] array = inputString.toCharArray();

        //loop through and reverse order until two vars meet in middle of array
        // while (indexStart&lt;=(length/2)) {
        // I replaced that with a for loop using two variables
        for (int indexStart=0, indexEnd=length-1; indexStart&lt;indexEnd; indexStart++,indexEnd--) {
            //swap array elements
            char temp = array[indexStart];
            array[indexStart] = array[indexEnd];
            array[indexEnd] = temp;
            //increment/decrement to move through arrays
            // That's done by the for loop, now.
            // indexStart++;
            // indexEnd--;
        }

        //convert back to string
        String newString = new String(array);
        return newString;
    }

}


But I'd write still another version of the program:

package test;

import java.util.Scanner;

public class ReverseString2 {

    /**
     * This method will read a String from standard input 
     * and print the character-reversed string to standard output.  
     * @param args
     */
    public static void main(String[] args) {
        //declare variables
        Scanner input = new Scanner(System.in);
        // Don't introduce variables before you need them
        // String inputString = "";
        // int length = 0, indexStart = 0, indexEnd = 0;

        //get input string
        System.out.print("Enter the string you want to reverse: ");
        String inputString = input.nextLine();

        String newString = reverseString(inputString);

        //output result
        System.out.println(newString);

        //close resources
        input.close();
    }

    /**
     * Returns the character-reversed String of the inputString
     * @param inputString the input String
     * @return the input string in reversed order of the characters
     */
    public static String reverseString(String inputString) {
        int length = inputString.length();
        char[] array = new char[length];

        for (int iSource=0,iTarget=length-1; iTarget&gt;=0; iSource++,iTarget--) {
            array[iTarget] = inputString.charAt(iSource);
        }

        String newString = new String(array);
        return newString;
    }

}