fun sortWordsInString(s: String): String {
// Create a list to hold the words
val words = s.split(" ") // Words are separated by spaces
// Enable automatic alphabetical sorting
val sorted = words.sorted()
// Optional: ignore duplicate words
// (Kotlin's `distinct()` removes duplicates)
val unique = sorted.distinct()
// Join the sorted words back into a single string
return unique.joinToString(" ")
}
fun main() {
// Test the function
println(sortWordsInString("the quick brown fox jumps over the lazy dog"))
}
/*
run:
brown dog fox jumps lazy over quick the
*/