-
-
Save Pasanpr/26e6e6227e62b7330a09a825c02a612f to your computer and use it in GitHub Desktop.
Determine if a character is a vowel or not
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extension Character { | |
var isVowel: Bool { | |
let vowels: [Character] = ["a", "e", "i", "o", "u"] | |
return vowels.contains(self) | |
} | |
} |
Here is mine using switch statement just to show another way, no way as elegant as the original but seemed to work fine!
func removeVowels(from: String) -> String {
var newString = ""
for each in from.characters {
switch each {
case "a", "e", "i", "o", "u": break
default: newString.append(each)
}
}
return newString
}
Sharing also my solution:
func removeVowels(from value: String) -> String {
return String(value.characters.filter { !["a", "e", "i", "o", "u"].contains($0) })
}
Sharing my solution:
func removeCharacters(_ charactersToRemove : [Character], myString : String) -> String {
let results = String(myString.characters.filter{!charactersToRemove.contains($0)})
return results
}
removeCharacters(["a", "e", "i", "o", "u"], myString: "Guardians of the Galaxy Vol. 2")
I added the uppercased versions to the Character Extension, as well as an isConsonant that just piggy backs off of isVowel so you don't have to loop through all the other characters.
extension Character {
var isVowel: Bool {
let vowels: [Character] = ["a", "e", "i", "o", "u", "A", "E", "I", "O", "U"]
return vowels.contains(self)
}
var isConsonant: Bool {
return !self.isVowel
}
}
func removeVowels(from string: String) -> String {
var result: String = ""
for character: Character in string.characters {
if character.isConsonant {
result.append(character)
}
}
return result
}
'characters' is deprecated in the newer versions of swift thus removeVowels function should:
func removeVowels(from value: String) -> String {
return String(value.filter { !["a", "e", "i", "o", "u"].contains($0) })
}
extension Character
{
var isVowel: Bool {
return "aeiou".contains(self)
}
}
func removeVowels(from: String) -> String
{
return from.filter { !$0.isVowel }
}
func removeVowels(from mystring: String) -> String {
let vowels:[Character] = ["a","e","i","o","u","y"]
var output = ""
for a in mystring {
if vowels.contains(a) != true {
output += String(a)
}
}
return output
}
func removeVowels(from value: String) -> String {
var vowels: [Character] = ["a", "e", "i", "o", "u"]
var newValue = value.lowercased()
newValue.removeAll(where: { vowels.contains($0) })
return newValue
}
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is my solution: