//而在Swift里面函数可以有多个返回值,返回值类型是元组类型,可以通过名称和下标访问元组元素,元组的成员不需要在函数中返回时命名,已经在函数返回类型中定义好了。
// 步骤
//实现此案例需要按照如下步骤进行。
//步骤一:定义一个返回元组类型的函数
//由于需要通过函数count同时得到一个字符串中元音、辅音以及其他字母的个数,所以count函数将返回一个元组类型,包含多个值,代码如下所示:
func count(string:String)->(vowels:Int, consonants:Int, others:Int) {
}
//步骤二:实现count函数
//count函数内部根据传入的字符串,依次判断字符串的每个字符是属于元音还是辅音,或是其他字符,代码如下所示:
func count (string:String)->(vowels:Int,consonants:Int,others:Int) {
string.lowercaseString
var vowels = 0
varconsonats = 0
var others = 0
for character in string {
switch String(character).lowercaseString {
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":
consonats++
default:
others++
}
}
return (vowels,consonats,others)
}