How to find the largest word from the string using C#? -


this code finding largest word given string. have got length of words in string how largest word printed out? have tried largest words not able using code plz help?

using system; using system.linq;  class largest1{     public void largest(){         console.writeline("enter string:");          string buffer1 = console.readline();         string[] buffer = buffer1.split(' ');         int length;         string largestword = buffer[0];          for(int = 0; < buffer.length; i++){             string temp = buffer[i];             length = temp.length;              if( largestword.length < buffer[i].length ) {                 largestword = buffer[i];             }         }          var largestwords = words in buffer                             let x =  largestword.length                             words.length == x                              select words;          console.write("largest words are:");               foreach(string s in largestwords){             console.write(s);         }     }      static void main(){         largest1 obj = new largest1();         obj.largest();     } } 

//put in class persisted string largestword = "";  //put right before loop largestword = buffer[0];  //put inside loop if( largestword.length < buffer[i].length ) {      largestword = buffer[i]; } 

edited: added support multiple words of same length

class largestwordsclass {     public largestwordsclass()     {         _largestwords = new list<string>();     }      //this says variable can set class read     public int largestwordsize     {         get;         private set;     }      //this lets users list without being able modify     private list<string> _largestwords;     public string[] largestwords     {                 {             return _largestwords.toarray();         }     }      public void findlargestword()     {         _largestwords.clear();          console.writeline("enter string: ");         string buffer = console.readline();         string[] splitbuffer = buffer.split(' ');          largestwordsize = 0;         (int = 0; < splitbuffer.length; i++)         {             if (largestwordsize < splitbuffer[i].length)             {                 largestwordsize = splitbuffer[i].length;                 _largestwords.clear();                 _largestwords.add(splitbuffer[i]);             }             else if (largestwordsize == splitbuffer[i].length)             {                 _largestwords.add(splitbuffer[i]);             }              console.writeline("the word " + splitbuffer[i] + " , length " + splitbuffer[i].length.tostring());         }          console.writeline("the largest word" + ((_largestwords.count > 1) ? "s are" : " is:"));         (int = 0; < _largestwords.count; i++)         {             console.writeline(_largestwords[i]);         }     } } 

Comments

Popular posts from this blog

linux - Mailx and Gmail nss config dir -

c# - Is it possible to remove an existing registration from Autofac container builder? -

php - Mysql PK and FK char(36) vs int(10) -