To find the kth longest word in a string, you can follow these steps:
Split
method.OrderByDescending
method.Here's the code snippet that implements the above algorithm:
main.cs258 chars7 lines
In the above code, the Split
method splits the input string into an array of words using a space character as a separator. The OrderByDescending
method sorts the resulting array in descending order by word length. The Skip
method skips the first k-1
elements of the sorted array, and the FirstOrDefault
method returns the kth element or null
if the array contains less than k
elements.
You can then call the above method with the input string and the desired value of k
:
main.cs162 chars6 lines
gistlibby LogSnag