题解 | 统计字符
统计字符
https://www.nowcoder.com/practice/539054b4c33b4776bc350155f7abd8f5
package main
import (
"bufio"
"fmt"
"os"
"unicode"
)
func main() {
scan := bufio.NewScanner(os.Stdin)
scan.Scan()
input := scan.Text()
chNum := 0
spaceNum := 0
numberNum := 0
otherNum := 0
for _, ch := range input {
if unicode.IsLetter(unicode.ToLower(ch)) {
chNum++
} else if unicode.IsDigit(ch) {
numberNum++
} else if ch == 32 {
spaceNum++
} else {
otherNum++
}
}
fmt.Println(chNum)
fmt.Println(spaceNum)
fmt.Println(numberNum)
fmt.Println(otherNum)
}

查看14道真题和解析