To find the kth longest word in a string in Swift, we can follow these steps:
CharacterSet
and components(separatedBy:)
method of String
class.components(separatedBy:)
method.sorted(by:)
method of Array
class.Here's the Swift code implementation of the above algorithm:
main.swift404 chars9 lines
We can test the above function with the following example:
main.swift204 chars5 lines
The output of the above code will be:
main.swift92 chars2 lines
gistlibby LogSnag