题解 | #统计字符#

统计字符

https://www.nowcoder.com/practice/539054b4c33b4776bc350155f7abd8f5

package main

import (
	"bufio"
	"fmt"
	"os"
	"unicode"
)

type Char int

const (
	Letter Char = iota
	Space
	Digit
	Other
)

func main() {
	inputs := bufio.NewScanner(os.Stdin)
	inputs.Scan()
	str := inputs.Text()

	var counts [4]int

	for _, r := range str {
		switch {
		case unicode.IsLetter(r):
			counts[Letter]++
		case unicode.IsSpace(r):
			counts[Space]++
		case unicode.IsDigit(r):
			counts[Digit]++
		default:
			counts[Other]++
		}
	}

	for _, count := range counts {
		fmt.Println(count)
	}
}

全部评论

相关推荐

点赞 评论 收藏
转发
点赞 收藏 评论
分享
牛客网
牛客企业服务