JadenCase ๋ฌธ์ž์—ด ๋งŒ๋“ค๊ธฐ - ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค(Swift)

2022. 10. 6. 14:21ใ†ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค-Swift

JadenCase ๋ฌธ์ž์—ด ๋งŒ๋“ค๊ธฐ - ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค(Swift)

 

 

 ๋ฌธ์ œ ์„ค๋ช…

 

https://school.programmers.co.kr/learn/courses/30/lessons/12951

 

ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค

์ฝ”๋“œ ์ค‘์‹ฌ์˜ ๊ฐœ๋ฐœ์ž ์ฑ„์šฉ. ์Šคํƒ ๊ธฐ๋ฐ˜์˜ ํฌ์ง€์…˜ ๋งค์นญ. ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค์˜ ๊ฐœ๋ฐœ์ž ๋งž์ถคํ˜• ํ”„๋กœํ•„์„ ๋“ฑ๋กํ•˜๊ณ , ๋‚˜์™€ ๊ธฐ์ˆ  ๊ถํ•ฉ์ด ์ž˜ ๋งž๋Š” ๊ธฐ์—…๋“ค์„ ๋งค์นญ ๋ฐ›์œผ์„ธ์š”.

programmers.co.kr

 

 

 ๊ธฐ์กด์˜ ๋‚˜์˜ ํ’€์ด

 

๊ณต๋ฐฑ์— ๋”ฐ๋ผ์„œ ๊ฐ๊ฐ์˜ ๋ฌธ์ž์—ด์— ๊ทœ์น™์„ ์ ์šฉํ–ˆ๋‹ค.

ํ•˜์ง€๋งŒ ์ด ํ’€์ด๋Š” ๊ณต๋ฐฑ์ด ์—ฌ๋Ÿฌ๊ฐœ์ผ๋•Œ ํ…Œ์ŠคํŠธ์ผ€์ด์Šค๋ฅผ ํ†ต๊ณผํ•˜์ง€ ๋ชปํ•œ๋‹ค.

์ฆ‰ abc    def๋ผ๋Š” ๋ฌธ์ž์—ด์ด ์žˆ์„๋•Œ solutionํ•จ์ˆ˜๋ฅผ ๊ฑฐ์น˜๋ฉด Abc    Def๊ฐ€ ์•„๋‹Œ Abc Def๊ฐ€ ๋œ๋‹ค.

๊ทธ๋ž˜์„œ ๊ณ ์ฐจํ•จ์ˆ˜๋ฅผ ์ž ์‹œ ๋‚ด๋ ค๋†“๊ณ  ๊ณต๋ฐฑ์˜ ๊ฐœ์ˆ˜๋ฅผ ์„ธ๊ธฐ ์œ„ํ•ด ๋‹ค๋ฅธ ์ฝ”๋“œ๋กœ ์ ‘๊ทผํ–ˆ๋‹ค.

 

func solution(_ s:String) -> String {
    let arr = s.split(separator: " ").map{String($0)}
    return arr.map{ $0.first!.isNumber ? $0.lowercased() : $0.capitalized }.joined(separator: " ")
}

 

 

 ๊ฐœ์„ ํ•œ ๋‚˜์˜ ํ’€์ด

 

func solution(_ s:String) -> String {
    
    var idx = 0
    var result = ""
    
    for x in s {
        if x == " " {
            result += String(x)
            idx = 0
        } else {
            if idx == 0 {
                result += String(x).uppercased()    
            } else {
                result += String(x).lowercased()
            }
            idx += 1
        }
    }
    return result
}

 

 

 ๋‹ค๋ฅธ ์‚ฌ๋žŒ์˜ ํ’€์ด

 

์ง๊ด€์ ์ธ ๋…ผ๋ฆฌ๋กœ ์ž‘์„ฑํ•˜์…”์„œ ๊ฐ€์ ธ์™”๋‹ค. 

๊ณต๋ฐฑ์„ uppercasedํ•˜๊ฑฐ๋‚˜ lowercasedํ•˜๋”๋ผ๋„ ๊ณต๋ฐฑ์ด๊ธฐ ๋•Œ๋ฌธ์— ์ „์˜ ๋นˆ์นธ์ด ๊ณต๋ฐฑ์ธ์ง€ ์•„๋‹Œ์ง€์— ๋”ฐ๋ผ ๋กœ์ง์„ ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์žˆ์—ˆ๋‹ค.

 

func solution(_ s:String) -> String {
    
    var isBeforeBlank: Bool = true
    var result = ""
    for x in s {
        result += isBeforeBlank ? x.uppercased() : x.lowercased()
        isBeforeBlank = (x == " ")
    }
    return result
}