Created
October 2, 2016 13:34
-
-
Save ibrahimkteish/72e11f378988abe39162d430358c7985 to your computer and use it in GitHub Desktop.
numer of vowels in string
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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