Java: How to find the longest word in a given String? -


in given string, want find longest word , print same.

the output second longest word, i.e "today", should "happiest".
may know doing wrong or there better/different way find longest word in string?

public class demostring  {     public static void main(string[] args)      {         string s="today happiest day of life";         string[] word=s.split(" ");         string rts=" ";         for(int i=0;i<word.length;i++){             for(int j=1+i;j<word.length;j++){                 if(word[i].length()>=word[j].length()){                     rts=word[i];                 }             }         }         system.out.println(rts);         system.out.println(rts.length());     } } 

instead should be:

for(int i=0;i<word.length;i++){    if(word[i].length()>=rts.length()){       rts=word[i];    } } 

Comments

Popular posts from this blog

commonjs - How to write a typescript definition file for a node module that exports a function? -

openid - Okta: Failed to get authorization code through API call -

ios - Change Storyboard View using Seague -