title: Find the longest word in a String
body: <p>Following is my code:</p>
  
  <pre><code> String LongestWord(String a)
  {
      int lw=0;
      int use;
      String lon="";
      while (!(a.isEmpty()))
      {
          a=a.trim();
          use=a.indexOf(" ");
          if (use&lt;0)
          {
              break;
          }
          String cut=a.substring(0,use);
          if(cut.length()&gt;lw)
          {
              lon=cut;
          }
          lw=lon.length();
          a=a.replace(cut," ");
      }
      return lon;
  }
  </code></pre>
  
  <p>The problem is that when I input a string like,
  "a boy is playing in the park"</p>
  
  <p>it returns the longest word as "ying" because when it replaces 'cut' with " " for the first time, it removes all the 'a'-s too, such that it becomes
  " boy is pl ying in the p rk" after the first iteration of the loop </p>
  
  <p>Please figure out what's wrong?</p>
  
  <p>Thanks in advance!</p>