Swift Data Structure And Algorithm/String Algorithm

๋Œ€์†Œ๋ฌธ์ž ๋ณ€ํ™˜

youngjaeLee1026 2022. 3. 10. 18:39

1. ๋ฌธ์ œ

แ„‰แ…ณแ„แ…ณแ„…แ…ตแ†ซแ„‰แ…ฃแ†บ 2022-03-10 18 40 33

2. ์ž…์ถœ๋ ฅ

แ„‰แ…ณแ„แ…ณแ„…แ…ตแ†ซแ„‰แ…ฃแ†บ 2022-03-10 18 40 51

3. ์ž…์ถœ๋ ฅ ์˜ˆ์‹œ

แ„‰แ…ณแ„แ…ณแ„…แ…ตแ†ซแ„‰แ…ฃแ†บ 2022-03-10 18 41 05

4. ๋ฌธ์ œ ์„ค๊ณ„

  1. ASCII ์ฝ”๋“œ ๊ฐ’ ๋น„๊ต๋ฅผ ํ†ตํ•ด ๋Œ€์†Œ๋ฌธ์ž๋ฅผ ๋ณ€ํ™˜
  2. 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()

์ „์ฒด์ฝ”๋“œ๋Š” ์—ฌ๊ธฐ์—์„œ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.