exercism/go/acronym/acronym.go

47 lines
927 B
Go
Raw Normal View History

2016-11-27 03:58:00 +00:00
package acronym
import "strings"
const testVersion = 1
func abbreviate(inp string) string {
var ret string
for _, wrd := range strings.Fields(inp) {
var newWrd []rune
for _, b := range wrd {
if (b >= 'A' && b <= 'Z') || (b >= 'a' && b <= 'z') {
newWrd = append(newWrd, b)
} else {
if len(newWrd) > 0 {
ret = ret + testWord(string(newWrd))
newWrd = []rune{}
}
}
}
ret = ret + testWord(string(newWrd))
}
return strings.ToUpper(ret)
}
// testWord figures out the abbreviation letters for
// the given word
func testWord(inp string) string {
if len(inp) == 0 {
return ""
}
if inp == strings.ToUpper(inp) {
// the whole word is capitalized, return
// the first letter
return string(inp[0])
}
var ret string
// Find uppercase characters to return
for i, s := range inp {
st := string(s)
if i == 0 || st == strings.ToUpper(st) {
ret = ret + st
}
}
return ret
}