Step-by-step solution for Longest Word
View the challenge

This challenge requires you to return the longest word in a sentence. Our goal is therefore to somehow loop through the string (what is a string?) and compare the lengths of the words. For example, if the string were "hello from coderbyte" the program should return the word "coderbyte" because it has the longest length. To solve this we need to: 1. Strip away any punctuation because the challenge tells us to ignore these symbols, so the string "hello$%()" is actually just "hello" 2. Separate the sentence into a list of words in order to easily retrieve words and their respective lengths 3. Then loop through this list and compare the words to find the one with the longest length We will be using regular expression functions in order to strip away punctuation and turn the string into a list of words. Regular expressions are patterns we define for the program to find in some text. Below is some code that performs each of these steps:

function LongestWord(sen) { 

  // we use the regex match function which searches the string for the
  // pattern and returns an array of strings it finds
  // in our case the pattern we define below returns words with
  // only the characters a through z and 0 through 9, stripping away punctuation
  // e.g. "hello$% ##all" becomes [hello, all]
  var arr = sen.match(/[a-z0-9]+/gi);

  // the array sort function takes a function as a parameter
  // which is used to compare each element in the array to the
  // next element in the array
  var sorted = arr.sort(function(a, b) {
    return b.length - a.length;
  });

  // this array now contains all the words in the original
  // string but in order from longest to shortest length
  // so we simply return the first element
  return sorted[0];
         
}
   
LongestWord("the $$$longest# word is coderbyte");                            
def LongestWord(sen): 

  # first we remove non alphanumeric characters from the string
  # using the translate function which deletes the specified characters
  sen = sen.translate(None, "~!@#$%^&*()-_+={}[]:;'<>?/,.|`")

  # now we separate the string into a list of words
  arr = sen.split(" ")

  # the list max function will return the element in arr
  # with the longest length because we specify key=len
  return max(arr, key=len)
    
print LongestWord("the $$$longest# word is coderbyte")  
def LongestWord(sen)

  # we use the regex match function which searches the string for the
  # pattern and returns an array of strings it finds
  # in our case the pattern we define below returns words with
  # only the characters a through z and 0 through 9, stripping away punctuation
  # e.g. "hello$% ##all" becomes [hello, all]
  arr = sen.split.map do |w| 
    /[a-zA-Z0-9\s]+/.match(w) 
  end
  
  # no need to sort and get the last word, you can
  # easily find largest word in array using max_by
  longest = arr.max_by do |w| 
      w.to_s.length 
  end
  
  return longest
         
end
   
puts LongestWord(STDIN.gets)
Top User Solutions
1 hyuil Challenge Score: 10 Cpp
1 cjordanball Challenge Score: 10 JavaScript
1 Qlogin Challenge Score: 10 Python
2 slavasprogis Challenge Score: 10 Python
1 emuchynski Challenge Score: 10 Ruby
3 KlaraKathrina Challenge Score: 10 Python
4 Sabin Challenge Score: 10 Python
2 junjunparkpark Challenge Score: 10 Ruby
5 Corinto Challenge Score: 10 Python
2 EmperorDuny Challenge Score: 10 JavaScript
6 gzamfire Challenge Score: 10 Python
7 mmalkavian Challenge Score: 10 Python
3 leot75 Challenge Score: 10 JavaScript
4 beeboop19 Challenge Score: 10 JavaScript
2 lokeshmogra Challenge Score: 10 Cpp
8 lesian Challenge Score: 10 Python
3 cdms0 Challenge Score: 10 Cpp
5 mattlarsh Challenge Score: 10 JavaScript
4 gutty333 Challenge Score: 10 Cpp
3 rnelson082 Challenge Score: 10 Ruby
6 jackgeek Challenge Score: 10 JavaScript
7 escapiststupor Challenge Score: 10 JavaScript
4 kmosier Challenge Score: 10 Ruby
9 hurgadion Challenge Score: 9 Python
10 scates Challenge Score: 10 Python
5 Bohdan Challenge Score: 10 Ruby
8 chrigolex Challenge Score: 10 JavaScript
6 lucanioi Challenge Score: 10 Ruby
5 sanguookchoi Challenge Score: 10 Cpp
7 SSJ6Porfy Challenge Score: 10 Ruby
8 stevenli Challenge Score: 10 Ruby
9 JoanneTisch Challenge Score: 10 JavaScript
10 archilolz Challenge Score: 10 JavaScript
9 zestybmac Challenge Score: 10 Ruby
1 arjnklc Challenge Score: 10 Java
10 youngheekwon Challenge Score: 10 Ruby
2 tonichen Challenge Score: 10 Java
6 johne Challenge Score: 10 Cpp
1 ionutforyou Challenge Score: 10 PHP
7 Justification Challenge Score: 10 Cpp
8 Zeikar Challenge Score: 10 Cpp
3 danaksim Challenge Score: 10 Java
4 jakmaj Challenge Score: 10 Java
5 mikejagen Challenge Score: 10 Java
6 AjeethKannan Challenge Score: 10 Java
7 ph4ntom Challenge Score: 10 Java
8 binfeng Challenge Score: 10 Java
9 anta0 Challenge Score: 10 Cpp
9 Niliyam Challenge Score: 10 Java
10 lingqyzeng Challenge Score: 10 Cpp
10 nicklivens Challenge Score: 10 Java
2 longcheng Challenge Score: 10 PHP
3 torbjornperttu Challenge Score: 10 PHP
4 albionselimaj Challenge Score: 10 PHP
5 teplicko Challenge Score: 10 PHP
6 smcdowell Challenge Score: 10 PHP
7 jetonr Challenge Score: 10 PHP
8 copacubanana Challenge Score: 10 PHP
9 mihainem Challenge Score: 9 PHP
10 khattab Challenge Score: 9 PHP

Become a Premium Member

With our large collection of challengs, tutorials, and solutions, we make it easy for you to become a better coder, prepare for interviews, and learn new skills from more experienced coders.

“Also important to try coding in nonstructured or semistructured environments to see if you like it. Coderbyte really helped me.“ ― Jeff Ruder
  • 200+ Coding Challenges
  • Mock Interview Questions
  • 500,000+ Code Solutions
  • Algorithm Tutorials
  • Interview Prep Courses