mercredi 4 janvier 2017

How to write a function that takes in a string and removes any consonants before the first vowel

So I am fairly new to Swift(trying to self-learn) , and I am already stuck. Im trying to create a function that takes in a string as a parameter, checks if there is any consonants before the first vowel, and removes it if so. So if the user inputs his name as Michael, ichael would print out. I have written

func shortNameForName(name: String) -> String {

let shortName = name.lowercaseString

let vowelSet = CharacterSet(charactersIn: "aeiou")

/* I know here i can create an if statement that basically states that if the first character of the string does not contain the vowelset, remove it, but I don't know the syntax/how to write it */

return shortName

}

print(shortNameForName("Michael"))

Thank you in advance!

Aucun commentaire:

Enregistrer un commentaire