Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import UIKit
- /*
- Problem:
- Write a function called numberOfVowels(in string: String) that returns the count of the total number of vowels in a string.
- Example: numberOfVowels("Hello World!") // 3
- Thoughts/assumptions:
- - will need to account for capitalization.
- - do not have to worry about whitespace in this problem.
- Test cases:
- - numberOfVowels(string: Hello World) // returns 3
- - numberOfVowels(string: "JaKE COnnerlY") // returns 4
- - numberOfVowels(string: "How much wood would a woodchuck chuck, if a woodchuck could chuck wood?") // returns 21
- - numberOfVowels(string: "How much woOd woUld a wOodchuck chuck, if A woodchuck coUld chuck wood?") //returns 21
- Approach:
- - I used an if statement on same problem previously, I'll use a switch this time around
- - make sure given string is lowercased
- - loop thru characters in string and switch char
- - check if the char is a vowel in switch case
- - set a variable to count up by 1 when a vowel is found
- */
- func numberOfVowels(string: String) -> Int{
- var vowelCount = 0
- for char in string.lowercased() {
- switch char {
- case "a","e","i","o", "u":
- vowelCount += 1
- default:
- continue
- }
- }
- return vowelCount
- }
- numberOfVowels(string: "How much woOd woUld a wOodchuck chuck, if A woodchuck coUld chuck wood?")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement