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

huangapple go评论55阅读模式
英文:

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

问题

我是新手学习Swift,正在研究函数。我有一个任务,如标题中所示,但我无法想象如何完成它。我需要创建一个函数,将所有元音字母转换为小写,所有辅音字母转换为大写,并将数字写成字母。

我期望它看起来像这样:

  1. 原始: "hello, I have been studying swift for 1 month"
  2. 结果: "hEllO, I hAvE bEEn stUdUIng swIft fOr OnE mOnth"

该函数应该接受一个字符串并返回一个字符串。

我尝试使用元组来实现,我有一个用于每个条件的数组元组,例如:

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

还有一个将数字转换为单词的字典:

let nums = [1:"one", 2:"two", 3:"three", 4:"fourth", 5:"five"]

但我仍然无法使其工作,不知道如何在函数内更改数组的元素。

英文:

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

答案1

得分: -2

只翻译代码部分:

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
}

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

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"))

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

(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)

huangapple
  • 本文由 发表于 2023年4月4日 18:17:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/75928183.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定