1、判断字符串中的汉字
- 判断字符串"hello 黎明,my name is 赤石"中汉字个数
var s = "hello 黎明,my name is 赤石"
func Han(a string) (count int) {
count = 0
for _, c := range a {
// 利用unicode.Is方法来判断字符类型
if unicode.Is(unicode.Han, c) {
count++
}
}
fmt.Printf("字符串s中汉字个数:%d", count)
return
}
func main() {
Han(s)
}
2、判断字符串中单词出现的次数
- “How do you do” 中每个单词的出现次数
var s = "How do you do"
func World(a string) (countMap map[string]int) {
countMap = make(map[string]int, 10)
s1 := strings.Split(a, " ")
for _, v := range s1 {
_, ok := countMap[v]
if !ok {
countMap[v] = 1
} else {
countMap[v] += 1
}
}
return
}
func main() {
fmt.Println(World(s))
}
3、回文判断
- 要求:判断是否是回文的代码
- 从左往右和从右往左读,结果是一样的,那么这个就是回文
- 例如
“爸爸是爸爸”;“上海自来水来自海上” 。。。
方法一:
var s = "上海自来水来自海上"
func Hunwen() {
var flag bool
tmpMap := make(map[int]int32, 10)
i := 0
for _, v := range s {
tmpMap[i] = v
i++
}
for n := 0; n < i; n++ {
if tmpMap[n