这个函数主要统计字母个数、数字个数、汉字和其他字符的个数(注意汉字和其他字符一起统计)
GO语言的代码有
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)