-1

The original question was "The function beginsWithVowel should take a single String parameter and return a Bool indicating whether the input string begins with a vowel. If the input string begins with a vowel return true, otherwise return false."

func lowercase(a: String) ->String{
    return a.lowercaseString
}

func lowercase(a: String) ->String{
    return a.lowercaseString
}
func beginsWithVowel(a: String) ->Bool {
    if  a.characters[a.startIndex] != ("a") && a.characters[a.startIndex] != ("e") && a.characters[a.startIndex] != ("i") && a.characters[a.startIndex] != ("o")  && a.characters[a.startIndex] != ("u")  {
        print("The word must start with a vowel letter.")
        return false
    }else {
        print("Succes!")
        return true
    }
}

When the a = ""

beginsWithVowel(lowercase(""))

An error occurred.

What should I add to make the function say reminder sentence instead of an error?

I have tried to add those into my code, but the error still occurred(ps: the func lowercase was added after fails)

a.characters[a.startIndex] != ("")

and

if a.characters.count == 0 {

}
rmaddy
  • 314,917
  • 42
  • 532
  • 579
Aye
  • 1
  • 1
  • 1
    Try not to adopt quirky habits like expressing your strings as `("x")` instead of just `"x"`. – tadman Nov 19 '17 at 03:33
  • You should also look to the `switch` statement as a way of dramatically simplifying that code. See [the documentation](https://developer.apple.com/library/content/documentation/Swift/Conceptual/Swift_Programming_Language/ControlFlow.html) for more. – tadman Nov 19 '17 at 03:34
  • Thanks for replying. I understand, but this exercise was designed just for If statement and basic function usage. – Aye Nov 19 '17 at 03:37
  • You should add a condition that tests for an empty string if you're having problems accessing characters within them. Make sure to `return` from that `if a.characters.count == 0` test or it'll just carry on to the next `if` as if nothing had happened, then error out. – tadman Nov 19 '17 at 03:39
  • See also https://stackoverflow.com/questions/44316086/got-error-return-an-empty-string-to-bool/44316177#44316177 – vadian Nov 19 '17 at 06:02

1 Answers1

0

You can simply return false if your string is empty otherwise create a string with the vowels and check if it contains the first character of your string:

Swift 3

func beginsWithVowel(a: String) -> Bool {
    return a.isEmpty ? false : "aeiouAEIOU".characters.contains(a.characters.first!)
}

Swift 4

func beginsWithVowel(a: String) ->Bool {
    return a.isEmpty ? false : "aeiouAEIOU".contains(a.first!)
}

beginsWithVowel(a: "Apple") // true

Note that it will return false for vowels with accent. If you would like to make your method diacritic insensitive you can use string's method func folding(options: String.CompareOptions = default, locale: Locale?) -> String to return the string without accent for comparison:

Swift 3

func beginsWithVowel(a: String) ->Bool {
    return a.isEmpty ? false : "aeiouAEIOU".characters.contains(a.folding(options: .diacriticInsensitive, locale: nil).characters.first!)
}

Swift 4

func beginsWithVowel(a: String) ->Bool {
    return a.isEmpty ? false : "aeiouAEIOU".contains(a.folding(options: .diacriticInsensitive, locale: nil).first!)
}

beginsWithVowel(a: "águia") // true
Leo Dabus
  • 229,809
  • 59
  • 489
  • 571
  • It appears an error "Value of type 'String' has no member 'contains' " – Aye Nov 19 '17 at 04:15
  • What is your Xcode version? Is it Swift 3?Just use contains in the string `characters` property instead – Leo Dabus Nov 19 '17 at 04:17
  • contains it is only available for the string type in Xcode 9 or later https://developer.apple.com/documentation/swift/string/2893238-contains – Leo Dabus Nov 19 '17 at 04:24