Created
November 1, 2017 09:28
-
-
Save ilyarmnzhdn/aa67838f392c532ede16861dfed93c42 to your computer and use it in GitHub Desktop.
Find the longest word in string (Swift 4)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func findLongestWord(word: String) -> String { | |
let wordArray = word.components(separatedBy: " ") | |
var maxLength = 0 | |
for index in 0..<(wordArray.count) { | |
if wordArray[index].count > maxLength { | |
maxLength = wordArray[index].count | |
} | |
} | |
return "\(maxLength)" | |
} | |
findLongestWord(word: "The quick brown fox jumped over the lazy dog") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Optimized code: