Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save dinneo/29e36311441162a6d7f093817578cdb8 to your computer and use it in GitHub Desktop.
Save dinneo/29e36311441162a6d7f093817578cdb8 to your computer and use it in GitHub Desktop.
numer of vowels in string
//First Implementation
func numberOfVowelsInString(string: String) -> Int {
let vowels: [Character] = ["a", "e", "i", "o", "u", "A", "E", "I", "O", "U"]
var numberOfVowels = 0
for character in string.characters {
if vowels.contains(character) {
numberOfVowels += 1
}
}
return numberOfVowels
}
//Second Implementation
func numberOfVowelsInString(string: String) -> Int {
let vowels: [Character] = ["a", "e", "i", "o", "u", "A", "E", "I", "O", "U"]
return string.characters.reduce(0) { $0 + (vowels.contains($1) ? 1 : 0) }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment