Question: I have to reverse the string "He is the one" to "one the is He". I have written some programs in Java but am looking for other best solutions. Suggest any possible ways to minimize the current program.

First approach:

```
class StringRev{
    public static void main(String args[]){
    String str = "He is the one";
    String temp = "";
    String finalString = "";
        for(int i =str.length()-1;i>=0;i--){
            temp +=i!=0?str.charAt(i):str.charAt(i)+" ";
            if(str.charAt(i) == ' '||i==0){
                for(int j=temp.length()-1;j>=0;j--){
                    finalString += temp.charAt(j);
                }
                temp = "";
            }
        }
            System.out.println(finalString);
    }
}

```

Second approach:

```
class StringRev2{
    public static void main(String args[]){
    String str[] = "He is the one".split(" ");
    String finalStr="";
        for(int i = str.length-1; i>= 0 ;i--){
            finalStr += str[i]+" ";
        }
        System.out.println(finalStr);
    }
}

```

Answer: 
There are many ways to do this, depending on what you're going for.

If you're stuck with Java, then probably look to Apache commons:

```
StringUtils.reverseDelimited(st, ' ');

```

Try using Scala if you can, which is nicer in so many ways:

```
st.split(" ").reverse.mkString(" ")

```
Here is a clean approach to reverse a string by its parts:

```
public class Main {

    // ReverseParts: reverses a string by words as opposed to characters.
    public static String ReverseParts(String input, String splitBy, String joinBy) {
        StringBuilder built = new StringBuilder();

        // Note: String.split uses regex. See its definition
        String[] list = input.split(splitBy);

        // Rejoin all the characters into a StringBuilder.
        for ( String part: list ) {
            built.insert(0, part);
            built.insert(0, joinBy);
        }

        // Remove the unnecessary 'joinBy bit'.
        built.delete(0, 1);

        // Get back the final string.
        return built.toString();
    }
    public static void main(String[] args) {
        final String input = "Hello  Johnny's World!";

        System.out.println("Result: '" + ReverseParts(input, " +", " ")+"'");
        // Output:
        // Result: 'World! Johnny's Hello'
    }
}

```