To find the k-th longest word in a string, we first need to break down the string into individual words. We can do this by splitting the string using the strings.Split()
function. Once we have a list of words, we can sort them in descending order based on their length. Finally, we can retrieve the k-th longest word from the sorted list.
Here is a sample code to achieve this in Go:
main.go729 chars34 lines
In this code, we use the strings.Fields()
function to split the input string into a slice of individual words. The sort.Slice()
function is used to sort the words slice in descending order based on length. Finally, we retrieve the k-th longest word from the sorted slice and return it. The main function demonstrates how to enter input string and k and retrieve the result.
gistlibby LogSnag