辅音和元音斯威夫特 [英] Consonants and Vowels Swift
问题描述
我是Swift的新手,任何人都可以向我解释为什么我会继续遇到这个问题。我正在使用Xcode 6.4,但这里是我的问题,我希望我清除它但我需要我的函数接受大字符串然后返回元组(numVowels,numConsonants)
计算以辅音开头的单词数/元音
返回元组并打印函数调用的结果。
我不需要它来计算字符数,只需要每个单词的第一个字符。我创建一个for循环,将所有内容切换为小写。但现在我被卡住了。
I'm new at Swift, could anyone explain to me why I'm keep getting this problem. I'm using Xcode 6.4, but here is my question I hope I cleared it up but I needed my Function to takes in Large String then returns Tuple(numVowels, numConsonants) Count the number of words that start with consonants/vowels Return the Tuple and print the result of the function call. I did not need for it to count characters, only first character of each word. I create an for loop which will switch everything to lowercase. But now I'm stuck.
func count(string: String) -> (Vowels:Int, Consonants:Int) {
var Vowels = 0, Consonants = 0
for character in string {
switch String(character).lowercaseString {
case "a","e","i","o","u":
++Vowels
case "b","c","d","e","f","g","h","j","k","l","m","n","p","q","r","s","t","v","w","x","y","z":
++Consonants
default: break
}
}
return (Vowels, Consonants)
}
推荐答案
如果您使用的是swift 2:
if you are using swift 2 :
只需更改此行
for character in string {
到
for character in string.characters {
它只会起作用罚款
count("hello") // (.0 2, .1 3)
这篇关于辅音和元音斯威夫特的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!