Forked from ibrahimkteish/numberOfVowelsInString.swift
Created
October 4, 2016 05:19
-
-
Save dinneo/29e36311441162a6d7f093817578cdb8 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