Improved word count

This commit is contained in:
Meutel 2017-07-07 16:13:45 +02:00
parent 675b497a70
commit 8b525e218e

View File

@ -2,19 +2,18 @@ package main
import "fmt"
import "io"
import "io/ioutil"
import "log"
import "os"
import "bufio"
import "strings"
func wc(r io.Reader) map[string]uint {
words := make(map[string]uint)
if text, err := ioutil.ReadAll(r); err != nil {
log.Fatalln(err)
} else {
for _, word := range strings.Fields(string(text)) {
words[word]++
}
s := bufio.NewScanner(r)
s.Split(bufio.ScanWords)
for s.Scan() {
word := strings.ToLower(s.Text())
words[word]++
}
return words
}