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();
}

```
**Approach 1:**

JDK provides `java.util.StringTokenizer` to do the split or tokenizing a given text. This API helps avoid going character by character. No external jar/libraries required. Tokenizing gives you each separated word as an array element that you could print in reverse. 

```
import java.util.ArrayList;
import java.util.StringTokenizer;

public class reverseString {
    public static void main(String args[])
    {
            StringTokenizer st = new StringTokenizer("this is a string");
            ArrayList<String> arrLstStrings = new ArrayList<>();
            while (st.hasMoreTokens()) {
                arrLstStrings.add(st.nextToken());
            }

            for(int loop=arrLstStrings.size()-1;loop>=0;loop--)
                System.out.println(arrLstStrings.get(loop));
    }
}

```

**Approach 2:**

```
import java.util.regex.*;

public class reverseString {
    public static void main(String args[])
{
        String strSource = "This is a string";
        // give a max int as limit in next stmt.
        String[] tokens = Pattern.compile(" ").split(strSource,15) ;
        for (int loop=tokens.length-1;loop>=0;loop--)
            System.out.println(tokens[loop]);
    }
}

```