title: How to compare words of one string in Java with words of another string and separate the words on match?
body: <p>This was an interview question.
  You are told to take string input from user &amp; output a string (or array of string) separated by spaces with meaningful words matching from another string called as Dictionary. You have a dictionary function to check if word exists or not.<br>
  For example:<br>
  if <code>Input is "howareyou"
  Output should be "how are you"</code>.<br>
  where the words 'how', 'are', 'you' are exist in dictionary string.    </p>
  
  <p><em>One more example:</em><br>
  <code>Input: "somethingneedstobedone
  Output: "something needs to be done</code>
  (Assuming that dictionary has words like <em>something, needs, to, be, done</em>.  </p>
  
  <p>I am not getting when to do k++ if there is no match.</p>
  
  <p>The code I tried:  </p>
  
  <pre><code>public class Sample1 {
  public static void main(String[] args) {
      Scanner sc=new Scanner(System.in);
      int i,j,k,len;
      String[] dict= {"how","are","you","something","needs","to","be","done"};
      //StringBuilder str=new StringBuilder("howareyou");
      StringBuilder str=new StringBuilder("somethingneedstobedone");
      len=str.length();
      for(i=0,j=0,k=0;i&lt;len;i++)
      {
          for(j=i+1;j&lt;len;j++)
          {
              if(dict[k].toString().equals(str.substring(i, j)))
              {
                  str.insert(j, " ");
                  k++;
              }
          }
      }
      System.out.println(str);
      sc.close();
  }
  </code></pre>
  
  <p>The commented case works well, but help me to get second case worked.</p>