Advertisement
Guest User

Untitled

a guest
Jul 21st, 2019
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.35 KB | None | 0 0
  1. import UIKit
  2.  
  3. /*
  4.  
  5. Problem:
  6. Write a function called numberOfVowels(in string: String) that returns the count of the total number of vowels in a string.
  7. Example: numberOfVowels("Hello World!") // 3
  8.  
  9. Thoughts/assumptions:
  10. - will need to account for capitalization.
  11. - do not have to worry about whitespace in this problem.
  12.  
  13. Test cases:
  14. - numberOfVowels(string: Hello World) // returns 3
  15. - numberOfVowels(string: "JaKE COnnerlY") // returns 4
  16. - numberOfVowels(string: "How much wood would a woodchuck chuck, if a woodchuck could chuck wood?") // returns 21
  17. - numberOfVowels(string: "How much woOd woUld a wOodchuck chuck, if A woodchuck coUld chuck wood?") //returns 21
  18.  
  19. Approach:
  20. - I used an if statement on same problem previously, I'll use a switch this time around
  21. - make sure given string is lowercased
  22. - loop thru characters in string and switch char
  23. - check if the char is a vowel in switch case
  24. - set a variable to count up by 1 when a vowel is found
  25. */
  26.  
  27. func numberOfVowels(string: String) -> Int{
  28. var vowelCount = 0
  29. for char in string.lowercased() {
  30. switch char {
  31. case "a","e","i","o", "u":
  32. vowelCount += 1
  33. default:
  34. continue
  35. }
  36. }
  37. return vowelCount
  38. }
  39.  
  40. 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