- 编写一个程序,要求可以把一个字符串中的每一个字符,如果是大写字母变小写字母,如果是小写字母变大写,如果是其他字符不变
- 例如: 字符串China23beiJing 经过程序转换为cHINA23BEIjING
func changeCharcter (chNum:Character) -> Character {
var chStr = String(chNum)
var num:UInt32 = 0
for item in chStr.unicodeScalars {
num = item.value
}
if num >= 65 && num <= 90 {
num += 32
}
else if num >= 97 && num <= 122 {
num -= 32
}
let newChNum = Character(UnicodeScalar(num)!)
return newChNum
}
var str = "China23beiJing "
var i = 0
while i < str.characters.count {
var str1 = str[str.index(str.startIndex, offsetBy: i)]
str.replaceSubrange(str.index(str.startIndex, offsetBy:
i)...str.index(str.startIndex, offsetBy: i),
with: String(changeCharcter(chNum: str1)))
i+=1;
}
print(str)