Skip to content

Instantly share code, notes, and snippets.

@AustinBCole
Created November 26, 2018 16:44
Show Gist options
  • Save AustinBCole/7992a09773a314ff9e6ae8edc21713e7 to your computer and use it in GitHub Desktop.
Save AustinBCole/7992a09773a314ff9e6ae8edc21713e7 to your computer and use it in GitHub Desktop.
Number of Vowels
Assumptions:
I am assuming that we are differentiating between letters that represent consonants and vowels and not between
vowel and consonant sounds. The letter "y" is not always considered a vowel and so I will not be counting it, nor will I
be counting the letter "w".
Test Cases:
"Hello World"//3
"Downton Abbey"//4
"Flooded doors don't flinch dumbly."//8
"Everyone Is Okay."//7
Approach:
A variable called "count" will be created and it will take an integer. I will create a for loop that iterates over all of the
letters in the string. I will create a switch statement within the for loop that tests for vowels. For each vowel, count will
plus equal one. I will then return count.
func numberOfVowels(string:String) -> Int {
//I lowercase the string to simplify the switch statement.
let myString = string.lowercased()
//This variable will count the vowels, it is the Int that I will be returning.
var count : Int = 0
//This for loop iterates over all of the letters in the lowercased string, the switch statements checks each letter to see if it is a vowel.
for letter in myString {
switch letter {
case "a":
count += 1
case "e":
count += 1
case "i":
count += 1
case "o":
count += 1
case "u":
count += 1
default:
break
}
}
return count
@dillon-mce
Copy link

Looks great Austin! You've definitely got a working solution and I can clearly see your thought process in the comments. You are missing a closing curly brace at the end, but I am assuming that was a transcription error. My only other suggestion would be that since your code is the same in all the cases, you could condense a little by putting them all on one line like so: case "a", "e", "i", "o", "u":

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment