Swift Data Structure And Algorithm/String Algorithm
๋์๋ฌธ์ ๋ณํ
youngjaeLee1026
2022. 3. 10. 18:39
1. ๋ฌธ์
2. ์ ์ถ๋ ฅ
3. ์ ์ถ๋ ฅ ์์
4. ๋ฌธ์ ์ค๊ณ
- ASCII ์ฝ๋ ๊ฐ ๋น๊ต๋ฅผ ํตํด ๋์๋ฌธ์๋ฅผ ๋ณํ
- Swift์์๋ Character์ ASCII ๊ฐ์ ๊ฐ์ ธ์ค๊ธฐ ์ํด์๋ character.asciiValue๋ฅผ ํตํด ๊ฐ์ ธ์ฌ ์ ์์ผ๋ฉฐ, ASCII -> Character๋ก ๋ณํํ๊ธฐ ์ํด์๋ Character(UnicodeScalar(.asciiValue))๋ฅผ ํตํด ๋ณํํ ์ ์์.
5. ์ ์ฒด ์ฝ๋
//
// main.swift
// ConvertAlpha
//
// Created by ์ด์์ฌ on 2022/03/10.
//MARK: - ๋์๋ฌธ์ ๋ณํ
//MARK: - Framework
import Foundation
//MARK: - Function
func solution() -> Void {
//MARK: - Input
guard let str: String = readLine() else { return }
var result: String = ""
//MARK: - Process
for char in str {
if 65 <= char.asciiValue! && char.asciiValue! <= 90 {
result += "\(Character(UnicodeScalar(char.asciiValue! + 32)))"
} else if 97 <= char.asciiValue! && char.asciiValue! <= 122 {
result += "\(Character(UnicodeScalar(char.asciiValue! - 32)))"
} else {
result += "\(char)"
}
}
//MARK: - Output
print(result)
}
solution()
์ ์ฒด์ฝ๋๋ ์ฌ๊ธฐ์์ ํ์ธํ ์ ์์ต๋๋ค.