【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
package main
 
import (
    "fmt"
    "os"
    "bufio"
    "strings"
)
 
func receiveStringsFromStdin() string {
    fmt.Printf("请输入一个字符串: ")
    reader := bufio.NewReader(os.Stdin)
    r, _ := reader.ReadString('\n')
    return r
}
 
func main() {
    var (
        s string
        count map[string]int
    )
    count = make(map[string]int)
    s = receiveStringsFromStdin()
    //fmt.Printf("输入的字符串为: %s", s)
    strArr := strings.Fields(s)
    for _, v := range strArr {
        _, e := count[v]
        if !e {
            count[v] = 1
        } else {
            count[v]++
        }
    }
 
    for k, v := range count {
        fmt.Printf("%s: %d次\n", k, v)
    }
}

  

posted @   kunsile  阅读(87)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 如何调用 DeepSeek 的自然语言处理 API 接口并集成到在线客服系统
· 【译】Visual Studio 中新的强大生产力特性
· 2025年我用 Compose 写了一个 Todo App
点击右上角即可分享
微信分享提示