Golang、python中统计字母,数字、汉字其余的个数。

这个函数主要统计字母个数、数字个数、汉字和其余字符的个数(注意汉字和其余字符一块儿统计)python

GO语言的代码有git

func main() {

   searchCount("Golang python")
   searchCount("我哼着" + "12345,54321" + "不当心踩了一坨屎,It smells good")

}
func searchCount(src string) {
   letters := "abcdefghijklmnopqrstuvwxyz"
   letters = letters + strings.ToUpper(letters)
   nums := "0123456789"

   numCount := 0
   letterCount := 0
   othersCount := 0

   for _, i := range src {
      switch {
      case strings.ContainsRune(letters, i) == true:
         letterCount += 1
      case strings.ContainsRune(nums, i) == true:
         numCount += 1
      default:
         othersCount += 1
      }

   }
   fmt.Println(letterCount, numCount, othersCount)
}

 

python代码简洁了一点函数

def  searchCount(src):
    numCount=0
    letterCount=0
    otherCount=0
    for i in src:
        if  i.isdigit():
            numCount+=1
        elif i.isalpha():
             letterCount+=1
        else:
            otherCount+=1
    print(letterCount,numCount,otherCount)

searchCount("Golang python")
a="我哼着" + "12345,54321" + "不当心踩了一坨屎,It smells good"
searchCount(a)