title: How to find the longest word in a given string?
body: <p>In a given string, I want to <strong>find the longest word</strong> then print it in the console.</p>
  <p>The output I get is the second longest word i.e <code>&quot;Today&quot;</code>, but I should get <code>&quot;Happiest&quot;</code> instead.<br />
  May I know what I am doing wrong? Is there a better/different way to find the longest word in a string?</p>
  <pre><code>public class DemoString {
      public static void main(String[] args) {
          String s = &quot;Today is the happiest day of my life&quot;;
          String[] word = s.split(&quot; &quot;);
          String longword = &quot; &quot;;
          for (int i = 0; i &lt; word.length; i++)
              for (int j = 1 + i; j &lt; word.length; j++)
                  if (word[i].length() &gt;= word[j].length())
                      longword = word[i];
  
          System.out.println(longword + &quot; is the longest word with &quot; + longword.length() + &quot; characters.&quot;);
          System.out.println(rts.length());
      }
  }
  </code></pre>