Skip to content

Instantly share code, notes, and snippets.

@mredig
Created July 10, 2019 16:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mredig/f0705bd9516171f482b92c506a23e0a1 to your computer and use it in GitHub Desktop.
Save mredig/f0705bd9516171f482b92c506a23e0a1 to your computer and use it in GitHub Desktop.
//: Playground - noun: a place where people can play
import Foundation
func numberOfVowels(in string: String) -> Int {
var numberOfVowels = 0
for vowel in string.lowercased() {
switch vowel {
case "a", "e", "i", "o", "u":
numberOfVowels = numberOfVowels + 1
default:
break //switch is done
}
}
return numberOfVowels
}
func numberOfVowels2(in string: String) -> Int {
let vowelArray = ["a", "e", "i", "o", "u"]
var numberOfVowels = 0
for letter in string.lowercased() {
let stringVersionOfLetter = String(letter)
if vowelArray.contains(stringVersionOfLetter) {
numberOfVowels += 1
}
}
return numberOfVowels
}
func numberOfVowels3(in string: String) {
let vowels = CharacterSet(charactersIn: "aeiou")
// let newString = string.replacingOccurrences(of: vowels, with: "")
let vowelCount = string.count - newString.count
print(vowelCount)
}
print(numberOfVowels(in: "Fee Fi Fo Fum"))
//print(numberOfVowels(in: "Hello world!"))
//print(numberOfVowels(in: "OBEY!"))
//print(numberOfVowels(in: "The lazy sheep jumped over the moon."))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment