How to replace the last occurrence of a character in a string with Go

1 Answer

0 votes
package main

import (
	"fmt"
	"strings"
)

func lengthOfLastWord(str string) int {
	words := strings.Fields(str)

	return len(words[len(words)-1])
}

func main() {
	str := "java c rust javascript golang"

	fmt.Println("The length of the last word is:", lengthOfLastWord(str))
}



/*
run:

The length of the last word is: 6

*/

 



answered Sep 9, 2024 by avibootz
edited Sep 9, 2024 by avibootz
...