欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

golang学习---练习4

程序员文章站 2022-04-27 08:16:36
...

第 0004 题:任一个英文的纯文本文件,统计其中的单词出现的个数。


分析:

  1. 去除非英文字符(空格除外)
  2. 以空格将文本分成一个个单词
  3. 以单词为key,value是当前统计的个数
  4. 打印

**解答 **

package main

import (
    "fmt"
    "strings"
)

const src = `Welcome to a tour of the Go programming language.

The tour is divided into a list of modules that you can access by clicking on A Tour of Go on the top left of the page.

You can also view the table of contents at any time by clicking on the menu on the top right of the page.

Throughout the tour you will find a series of slides and exercises for you to complete.

You can navigate through them using

"previous" or PageUp to go to the previous page,
"next" or PageDown to go to the next page.
The tour is interactive. Click the Run button now (or type shift-enter) to compile and run the program on a remote server. The result is displayed below the code.

These example programs demonstrate different aspects of Go. The programs in the tour are meant to be starting points for your own experimentation.

Edit the program and run it again.

Note that when you click on Format or ctrl-enter the text in the editor is formatted using the gofmt tool. You can switch on and off syntax highlighting the clicking on syntax button.

When you're ready to move on, click the right arrow below or type the PageDown key.`

func main() {
    //全部转换小写
    srcTmp := []byte(strings.ToLower(src))
    for i := 0; i < len(src); i++ {
        if srcTmp[i] > 'z' || srcTmp[i] < 'a' {
            srcTmp[i] = ' '
        }
    }

    //分隔" ", 成为数组
    arr := strings.Split(string(srcTmp), " ")

    //记录每个单词
    m := make(map[string]int)
    for _, value := range arr {
        if value != "" {
            m[value]++
        }

    }
    for key, value := range m {
        fmt.Println("-", key, "-", value)
    }

}
- tour - 6
- or - 5
- shift - 1
- demonstrate - 1
- re - 1
- can - 4
- for - 2
- through - 1
- edit - 1
- enter - 2
- remote - 1
- example - 1
- access - 1
- table - 1
- any - 1
- menu - 1
- right - 2
- are - 1
- again - 1
- ready - 1
- clicking - 3
- slides - 1
- pageup - 1
- a - 5
- the - 25
- programming - 1
- into - 1
- modules - 1
- next - 2
- pagedown - 2
- programs - 2
- of - 8
- list - 1
- view - 1
- contents - 1
- it - 1
- time - 1
- now - 1
- server - 1
- below - 2
- experimentation - 1
- go - 5
- is - 4
- also - 1
- previous - 2
- button - 2
- ctrl - 1
- these - 1
- be - 1
- divided - 1
- you - 8
- page - 4
- series - 1
- type - 2
- off - 1
- syntax - 2
- throughout - 1
- displayed - 1
- different - 1
- format - 1
- switch - 1
- that - 2
- will - 1
- result - 1
- your - 1
- move - 1
- welcome - 1
- language - 1
- them - 1
- using - 2
- interactive - 1
- arrow - 1
- key - 1
- and - 4
- compile - 1
- starting - 1
- when - 2
- at - 1
- navigate - 1
- click - 3
- note - 1
- editor - 1
- own - 1
- formatted - 1
- by - 2
- top - 2
- find - 1
- code - 1
- meant - 1
- program - 2
- aspects - 1
- points - 1
- to - 9
- on - 9
- left - 1
- exercises - 1
- complete - 1
- gofmt - 1
- tool - 1
- run - 3
- in - 2
- text - 1
- highlighting - 1

特殊字符没有处理,比如shift-enter就被分成两个字符处理