package main
import (
"fmt"
"strings"
)
func shortestWordLength(text string) int {
// Split on whitespace; Fields automatically removes empty entries
words := strings.Fields(text)
if len(words) == 0 {
return 0
}
minLen := len(words[0])
for _, w := range words {
if len(w) < minLen {
minLen = len(w)
}
}
return minLen
}
func main() {
input := "Find the shortest word length in this string"
result := shortestWordLength(input)
if result == 0 {
fmt.Println("No words found.")
} else {
fmt.Println("Length of the shortest word:", result)
}
}
/*
run:
Length of the shortest word: 2
*/