How to sort the words in a string with Go

1 Answer

0 votes
package main

import (
	"fmt"
	"sort"
	"strings"
)

func sortTheWordsInAString(s string) string {
	arr := strings.Fields(s)

	sort.Strings(arr)

	return strings.Join(arr, " ")
}

func main() {
	s := "php c java go c++ python c#"

	s = sortTheWordsInAString(s)

	fmt.Println(s)
}



/*
run:

c c# c++ go java php python

*/

 



answered Jul 30, 2024 by avibootz
...