加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

统计字符类型

发布时间:2020-12-14 02:22:23 所属栏目:百科 来源:网络整理
导读:使用Swift语言实现,非常简单,具体代码如下: func countChars(string: String) - (vowels: Int,consonants: Int,others: Int) { var vowels = 0,consonants = 0,others = 0 for character in string { var char = String(character).lowercaseString switc

使用Swift语言实现,非常简单,具体代码如下:

func countChars(string: String) -> (vowels: Int,consonants: Int,others: Int) {
    var vowels = 0,consonants = 0,others = 0
    for character in string {
        var char = String(character).lowercaseString
        switch char {
        case "a","e","i","o","u":
            vowels++
        case "b","c","d","f","g","h","j","k","l","m","n","p","q","r","s","t","v","w","x","y","z":
            consonants++
        default:
            others++
        }
    }
    
    return (vowels,consonants,others)
}
let charsInfo = countChars("some arbitrary string!")
println("Vowels:(charsInfo.vowels),Consonants:(charsInfo.consonants),Othes:(charsInfo.others)")


另外:

这代码不是我写的,是官方的例子。我做了一点微小的改动而已。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读