码迷,mamicode.com
首页 > 其他好文 > 详细

统计字符类型

时间:2015-02-27 00:22:02      阅读:132      评论:0      收藏:0      [点我收藏+]

标签:统计   swift   

使用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)")


另外:

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

统计字符类型

标签:统计   swift   

原文地址:http://blog.csdn.net/ccpw_cn/article/details/43957725

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!