设为首页 加入收藏

TOP

统计字符类型
2015-07-20 17:15:20 来源: 作者: 【 】 浏览:2
Tags:统计 字符 类型

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


另外:

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

】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇hdu 1847 Good Luck in CET-4 Eve.. 下一篇POJ 1375 Intervals

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容:

·Redis on AWS:Elast (2025-12-27 04:19:30)
·在 Spring Boot 项目 (2025-12-27 04:19:27)
·使用华为开发者空间 (2025-12-27 04:19:24)
·Getting Started wit (2025-12-27 03:49:24)
·Ubuntu 上最好用的中 (2025-12-27 03:49:20)