Find Out How Many Number of Vowels & Vowels in a Sentence & Remove duplicate elements
func findVowelAndCount(str: String) -> (vowelCount: Int, vowelStr: String) {
let vowels = "aeious"
var vowelsCounts = 0
var vowelsArray: [String] = []
var volsStr = ""
for i in str.lowercased(){
if vowels.contains(i){
vowelsCounts += 1
if !vowelsArray.contains("\(i)"){
vowelsArray.append("\(i)")
}
}
volsStr = vowelsArray.joined()
}
return (vowelsCounts, volsStr)
}
//MARK: - Output
let vowelsOutput = findVowelAndCount(str: "How to convert character array into string in Swift?")
print(vowelsOutput) //(vowelCount: 16, vowelStr: "oeais")
Comments
Post a Comment