How to get the first letter of each word in a string with Go

1 Answer

0 votes
package main

import (
	"fmt"
	"strings"
)

func getFirstLetters(s string) string {
	words := strings.Split(s, " ")
	firstLetters := ""

	for _, word := range words {
		if len(word) > 0 {
			firstLetters += string(word[0])
		}
	}

	return firstLetters
}

func main() {
	str := "go javascript php c typescript node.js"

	fmt.Println(getFirstLetters(str))
}



/*
run:

gjpctn

*/

 



answered Sep 19, 2024 by avibootz
...