-
Notifications
You must be signed in to change notification settings - Fork 0
/
wordCount.go
39 lines (30 loc) · 723 Bytes
/
wordCount.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
/*
@author - Ivan Liljeqvist
@version - 23-03-2015
*/
package main
import (
"golang.org/x/tour/wc"
"strings"
)
/*
This program includes a function that counts the number of times a string contains each word.
*/
/*
This function counts the number of times the string that is passed in as the parameter contains each word.
The results are returned as a map.
*/
func WordCount(s string) map[string]int {
//extract words from the string
words := strings.Fields(s)
//initialize the array that will be returned
wordCount := make(map[string]int)
//loop through the words, increase the count of words
for _, word := range words {
wordCount[word]++
}
return wordCount
}
func main() {
wc.Test(WordCount)
}