forked from duliodenis/swift-coding-challenges
-
Notifications
You must be signed in to change notification settings - Fork 0
/
10_vowelsConsonants.swift
41 lines (35 loc) · 1.22 KB
/
10_vowelsConsonants.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
// Challenge 10: Vowels and Consonants
// Write a function that returns a tuple containing the number of vowels and consonants.
func vowelConsonants(_ input: String) -> (vowels: Int, consonants: Int) {
let vowels = "aeiou".characters
let consonants = "bcdfghjklmnpqrstvwxyz".characters
var vowelCount = 0
var consonantCount = 0
for letter in input.lowercased().characters {
if consonants.contains(letter) {
consonantCount += 1
} else {
// check again to weed out punctuations
if vowels.contains(letter) {
vowelCount += 1
}
}
}
return (vowelCount, consonantCount)
}
print("---------------")
print(vowelConsonants("hello, world")) // (3, 7)
print(vowelConsonants("Mississippi")) // (4, 7)
//Swift 4
func vowelConsonants2(_ str: String) -> (vowels: Int, consonants: Int) {
var numOfVow = 0
var numOfCon = 0
str.lowercased().forEach {
if "bcdfghjklmnpqrstvwxyz".contains($0) { numOfCon += 1 }
else if "aeiou".contains($0) { numOfVow += 1 }
}
return (numOfVow, numOfCon)
}
print("---------------")
print(vowelConsonants2("hello, world")) // (3, 7)
print(vowelConsonants2("Mississippi")) // (4, 7)