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: 
In your first approach you have the right idea of starting at the end of the String and working backwards. The issues you have are:

* you should use StringBuilder instead of String concatenation....
* using a `temp` string which you add-to in reverse order, then reverse again in to the result is a slow approach .... but effective.
* you should be doing it more than 1 character at a time.

In your second approach, I don't like:

* you have string concatenation again **use** a `StringBuilder()`
* you assume all spaces are 'equal'... what about multi-space breaks `"Hello There"` with this code will become `"There Hello"`

So, with a small adjustment to your regular expression (perhaps `"\\b"` ...) and conversion to StringBuilder, I think the second option is good.

The first option, if written right, will be faster than the split one (although the code is longer)....

Here's an attempt at doing it by first principles:

```
private static String reverse(String string) {
    if (string.isEmpty()) {
        return string;
    }
    int last = string.length();
    StringBuilder sb = new StringBuilder(string.length());
    boolean contextspace = ' ' == string.charAt(string.length() - 1);
    for (int i = string.length() - 1; i >= 0; i--) {
        if (contextspace != (string.charAt(i) == ' ')) {
            sb.append(string.substring(i + 1, last));
            last = i + 1;
            contextspace = !contextspace;
        }
    }
    sb.append(string.substring(0, last));
    return sb.toString();
}

```
Re-use existing code.
---------------------

Libraries allow for maximum re-use. Use them!

If you don't like libraries, try to re-use as much as Java offers. Look up documentation, examples, etc.

* Splitting strings is java default `split()`
* Reversing can be done with the excellent `Collections` class
* Java8 has some nice String join stuff.

I also try to make the code as readable as possible by having short statements and avoiding loops (for, while, etc) when you don't need them.

My approach:

```
public static String reverseWords (String s) 
{
    String delimiter = " ";
    List<String> words = Arrays.asList(s.split(delimiter));
    Collections.reverse(words);
    return String.join(delimiter, words);
}

public static void main(String[] args) 
{
    System.out.println(reverseWords("He is the one"));
} 

```