package main
import (
"fmt"
"strings"
)
// getLastWord returns the last word in a string.
// Empty or whitespace-only strings return an empty string.
func getLastWord(s string) string {
// Trim leading/trailing whitespace
s = strings.TrimSpace(s)
// If nothing remains, return empty string
if s == "" {
return ""
}
// Split into fields (handles multiple spaces cleanly)
parts := strings.Fields(s)
// Return the last element
return parts[len(parts)-1]
}
func main() {
tests := []string{
"vb.net javascript php c c++ python c#",
"",
"c#",
"c c++ java ",
" ",
}
for i, t := range tests {
fmt.Printf("%d. %s\n", i+1, getLastWord(t))
}
}
/*
run:
1. c#
2.
3. c#
4. java
5.
*/