Skip to content

Instantly share code, notes, and snippets.

@ibrahimkteish
Created October 2, 2016 13:34
Show Gist options
  • Save ibrahimkteish/72e11f378988abe39162d430358c7985 to your computer and use it in GitHub Desktop.
Save ibrahimkteish/72e11f378988abe39162d430358c7985 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