-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
77 lines (71 loc) · 1.24 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
package main
import (
"bytes"
"fmt"
"os"
"strings"
)
var CODES = map[string]string{
"A": "Alpha",
"B": "Bravo",
"C": "Charlie",
"D": "Delta",
"E": "Echo",
"F": "Foxtrot",
"G": "Golf",
"H": "Hotel",
"I": "India",
"J": "Juliett",
"K": "Kilo",
"L": "Lima",
"M": "Mike",
"N": "November",
"O": "Oscar",
"P": "Papa",
"Q": "Quebec",
"R": "Romeo",
"S": "Sierra",
"T": "Tango",
"U": "Uniform",
"V": "Victor",
"W": "Whiskey",
"X": "X-ray",
"Y": "Yankee",
"Z": "Zulu",
}
func LetterToCode(letter string) string {
code := letter
if strings.ToLower(letter) == letter {
code = strings.ToUpper(letter)
}
if val, ok := CODES[code]; ok {
return val
}
return code
}
func WordToCode(word string) string {
var buffer bytes.Buffer
for index, character := range word {
letter := string(character)
if letter == " " { // don't process spaces
continue
}
code := LetterToCode(letter)
space := " "
if index+1 == len(word) { // skip trailing space
space = ""
}
buffer.WriteString(code + space)
}
return buffer.String()
}
func main() {
if len(os.Args) < 2 {
fmt.Printf("usage: %s hello world\n", os.Args[0])
os.Exit(1)
}
for i := 1; i < len(os.Args); i++ {
word := os.Args[i]
fmt.Println(WordToCode(word))
}
}