這個函數主要統計字母個數、數字個數、漢字和其餘字符的個數(注意漢字和其餘字符一塊兒統計)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)