我目前正在使用Xcode 10.1,并试图计算给定句子中的元音和辅音数量。我全局声明了常量
let sentence = "Here is my sentence"
然后,我尝试使用名称为"sentenceInput"
的参数调用函数,该函数在某些情况下将语句添加到函数中,这意味着对元音和辅音的数量进行计数并返回Int值。但是,在调用该函数时,我被告知只有1
个辅音和0
个元音。一般来说,对于编程和Xcode还是陌生的,我将非常感谢您的帮助。谢谢。功能代码:
func findVowelsConsonantsPunctuation(sentenceInput: String) -> (Int, Int, Int) {
var vowels = 0; var consonants = 0; var punctuation = 0
for character in sentenceInput.characters{
switch String(character).lowercased
{
case "a", "e", "i", "o", "u":
vowels += 1
case ".","!",":",";","?", " ", "'", "":
punctuation += 1
default:
consonants += 1
}
return (vowels, consonants, punctuation)
}
}
答案 0 :(得分:1)
请在下面尝试。您的for循环中有return
条语句。因此,您将在第一次迭代后返回。我也将您的lowercased()
移到了for循环上方。这样,每次迭代时小写字符的处理时间就会减少。
func findVowelsConsonantsPunctuation(sentenceInput: String) -> (Int, Int, Int) {
var vowels = 0; var consonants = 0; var punctuation = 0
sentenceInput = sentenceInput.lowercased()
for character in sentenceInput.characters {
switch character {
case 'a', 'e', 'i', 'o', 'u':
vowels += 1
case '.','!',':',';','?', ' ', '\'', '':
punctuation += 1
default:
consonants += 1
}
}
return (vowels, consonants, punctuation)
}
答案 1 :(得分:1)
您将return
语句放入循环中,因此,第一次执行循环代码时,该函数将返回。这就是为什么只有一个辅音的原因,因为switch case语句仅执行一次。
您需要将return
语句放在循环的外部之外,如下所示:
func findVowelsConsonantsPunctuation(sentenceInput: String) -> (Int, Int, Int) {
var vowels = 0
var consonants = 0
var punctuation = 0
for character in sentenceInput.characters {
switch String(character).lowercase {
case "a", "e", "i", "o", "u":
vowels += 1
case ".","!",":",";","?", " ", "'", "":
punctuation += 1
default:
consonants += 1
}
}
return (vowels, consonants, punctuation)
}
答案 2 :(得分:1)
我建议您阅读Set
。
请记住,您可以使用CharacterSet
并创建3套。
// Make your vowels
let vowels = CharacterSet(charactersIn: "aeiouy")
let consonants = CharacterSet.letters.subtracting(vowels)
let punctuation = CharacterSet.punctuationCharacters
然后,您要跟踪元音,辅音和标点的计数:
// Make your vowels
let vowels = CharacterSet(charactersIn: "aeiouy")
let consonants = CharacterSet.letters.subtracting(vowels)
let punctuation = CharacterSet.punctuationCharacters
然后遍历它。
func sentenceComponents(string: String) -> (Int, Int, Int) {
// Make your vowels
let vowels = CharacterSet(charactersIn: "aeiouy")
let consonants = CharacterSet.letters.subtracting(vowels)
let punctuation = CharacterSet.punctuationCharacters
// Set up vars to track our coutns
var vowelCount = 0
var consonantCount = 0
var punctuationCount = 0
string.forEach { char in
// Make a set of one character
let set = CharacterSet(charactersIn: String(char))
// If the character is a member of a set, incremennt the relevant var
if set.isSubset(of: vowels) { vowelCount += 1 }
if set.isSubset(of: consonants) { consonantCount += 1 }
if set.isSubset(of: punctuation) { punctuationCount += 1 }
}
return (vowelCount, consonantCount, punctuationCount)
}
let testString = "The quick brown fox jumped over the lazy dog."
sentenceComponents(string: testString)
更新:更整洁易读(也许?)
我不能忍受未标记的元组,所以这里有一个typealias
的更新,它告诉您您所要获得的内容,而不必越过河流和穿过树林来弄清楚元组中的内容:
typealias SentenceComponents = (vowels: Int, consonants: Int, punctuation: Int)
func components(in string: String) -> SentenceComponents {
// Make your vowels
let vowels = CharacterSet(charactersIn: "aeiouy")
let consonants = CharacterSet.letters.subtracting(vowels)
let punctuation = CharacterSet.punctuationCharacters
// Set up vars to track our coutns
var vowelCount = 0
var consonantCount = 0
var punctuationCount = 0
string.forEach { char in
// Make a set of one character
let singleCharSet = CharacterSet(charactersIn: String(char))
// If the character is a member of a set, incremennt the relevant var
if singleCharSet.isSubset(of: vowels) { vowelCount += 1 }
if singleCharSet.isSubset(of: consonants) { consonantCount += 1 }
if singleCharSet.isSubset(of: punctuation) { punctuationCount += 1 }
}
return (vowelCount, consonantCount, punctuationCount)
}
let testString = "Smokey, this is not 'Nam. This is bowling. There are rules."
// (vowels 17, consonants 27, punctuation 5)
components(in: testString)
答案 3 :(得分:0)
func findVowelsConsonants (_ sentence: String)-> (Int, Int){
let sentenceLowercase = sentence.lowercased()
var tuple = (numberOfVowels: 0, numberOfConsonants: 0, numberOfPunctuation: 0)
for char in sentenceLowercase{
switch char{
case "a", "e", "i", "o", "u":
tuple.0 += 1
case "b", "c", "d", "f", "g", "h", "j", "k", "l", "m", "n", "p", "q", "r", "s", "t", "v", "w", "x", "y", "z":
tuple.1 += 1
default:
tuple.2 += 1
}
}
return (tuple.0, tuple.1)
}