Search code examples
arraysswiftfunc

how do I make a func that will make all vowel letters lowercase, all consonants uppercase, and write numbers in letters?


I'm new to swift and studying functions rn. I have a task that's given in the title, and I can't really imagine how to do it. I need to make a func that turn all the vowel letters into lowercase, all consonants into uppercase and write numbers in letters

what I expect it to look like:

  1. original: "hello, I have been studying swift for 1 month"
  2. the result: "hEllO, I hAvE bEEn stUdUIng swIft fOr OnE mOnth"

the func should accept a string and return a string

I tried doing it with tuples, I had one tuple of arrays for each condition, for example:

var lowercase: [Character] = ["a", "e", "i", "o", "e", "y"]
    uppercase: [Character] = ["A", "E", "I", "O", "E", "Y"]

and also a dictionary to turn numbers into words: let nums = [1:"one", 2:"two", 3:"three", 4:"fourth", 5:"five"]

but I still can't make it work, don't know how to change arrays' elements inside the func


Solution

  • There may be many ways to achive the desired result.

    func transformString(_ input: String) -> String {
        let vowels = "aeiouAEIOU"
        
        var newString = ""
    
        for char in input {
            if vowels.contains(char) {
                newString.append(char.lowercased())
            } else {
                newString.append(char.uppercased())
            }
        }
        
        return newString
    }
    

    output

    print(transformString("hello, I have been studying swift for 1 month"))
    

    enter image description here

    (in ur question u says u want to convert vowels to lowercase and others to uppercase but in expected result it is in vice versa. So adjust the answer according to ur scenario)