题解 | #统计字符#
统计字符
https://www.nowcoder.com/practice/539054b4c33b4776bc350155f7abd8f5
package main import ( "fmt" "os" "bufio" ) func calculateLetter(s string) { var letterCnt, blankCnt, digitCnt, otherCnt int for i:=0; i<len(s); i++ { if ('A' <= s[i] && s[i] <= 'Z') || ('a' <= s[i] && s[i] <= 'z') { letterCnt++ } else if s[i] == ' ' { blankCnt++ } else if '0' <= s[i] && s[i] <= '9' { digitCnt++ } else { otherCnt++ } } fmt.Printf("%d\n%d\n%d\n%d", letterCnt, blankCnt, digitCnt, otherCnt) } func main() { var s string inputReader := bufio.NewReader(os.Stdin) line, _, _ := inputReader.ReadLine() s = string(line) calculateLetter(s) }
// 本题输入为一行字符串,包含空格,所以采用:inputReader.ReadLine()