Longest Word

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.

My solution:


Thought Process:

  • 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.

Screen Shot 2016-09-07 at 8.35.04 PM.png


Longest Word

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s