-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcharcount.go
More file actions
81 lines (76 loc) · 1.5 KB
/
charcount.go
File metadata and controls
81 lines (76 loc) · 1.5 KB
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
78
79
80
81
// Computes counts of characters
package main
import (
"bufio"
"fmt"
"io"
"os"
"unicode"
"unicode/utf8"
)
func main() {
charcount()
wordfreq()
}
func charcount() {
counts := make(map[rune]int)
types := make(map[string]int)
var utflen [utf8.UTFMax + 1]int
invalid := 0
in := bufio.NewReader(os.Stdin)
for {
r, n, err := in.ReadRune()
if err == io.EOF {
break
}
if err != nil {
fmt.Fprintf(os.Stderr, "charcount: %v\n", err)
os.Exit(1)
}
if r == unicode.ReplacementChar && n == 1 {
invalid++
continue
}
if unicode.IsLetter(r) {
types["letter"]++
} else if unicode.IsDigit(r) {
types["digit"]++
} else if unicode.IsSymbol(r) {
types["symbol"]++
} else if unicode.IsControl(r) {
types["control"]++
}
counts[r]++
utflen[n]++
}
fmt.Printf("type\tcount\n")
for t, n := range types {
fmt.Printf("%s\t%d\n", t, n)
}
fmt.Printf("\nrune\tcount\n")
for c, n := range counts {
fmt.Printf("%q\t%d\n", c, n)
}
fmt.Printf("\nlen\tcount\n")
for i, n := range utflen {
if i > 0 {
fmt.Printf("%d\t%d\n", i, n)
}
}
if invalid > 0 {
fmt.Printf("\n%d invalid UTF-8 characters\n", invalid)
}
}
func wordfreq() {
counts := make(map[string]int)
in := bufio.NewScanner(os.Stdin)
in.Split(bufio.ScanWords)
for in.Scan() {
word := in.Text()
counts[word]++
}
fmt.Printf("word\tcount\n")
for w, n := range counts {
fmt.Printf("%s\t%d\n", w, n)
}
}