Write a method that takes in a string. Return the longest word in the string. You may assume that the string contains only letters and spaces.
- Sentence = “hello you motherfucker”
- Break sentence into an array of words: words = “hello, you, motherfucker”
- Set the initial longest word as nil.
- Go through array (while loop). If the new current word is longer than the previous current word, set the longest word as the new current word
A simpler alternative:
Setting the initializer for longest_word as an empty string, whose length will be 0.