์ž๋ฐ” ์•Œ๊ณ ๋ฆฌ์ฆ˜ ๋ฌธ์ œ ํ’€์ด ์ž…๋ฌธ. ์„น์…˜1. String (๋ฌธ์ž์—ด) : ๋Œ€์†Œ๋ฌธ์ž ๋ณ€ํ™˜

2023. 10. 3. 20:30ใ†์ธํ”„๋Ÿฐ/์ž๋ฐ” ์•Œ๊ณ ๋ฆฌ์ฆ˜ ๋ฌธ์ œํ’€์ด ์ž…๋ฌธ : ์ฝ”๋”ฉํ…Œ์ŠคํŠธ ๋Œ€๋น„

728x90

 

https://hyejin.tistory.com/1197

 

์ž๋ฐ” ์•Œ๊ณ ๋ฆฌ์ฆ˜ ๋ฌธ์ œ ํ’€์ด ์ž…๋ฌธ : ์„น์…˜1. String (๋ฌธ์ž์—ด) : ๋ฌธ์ž ์ฐพ๊ธฐ

https://www.inflearn.com/course/%EC%9E%90%EB%B0%94-%EC%95%8C%EA%B3%A0%EB%A6%AC%EC%A6%98-%EB%AC%B8%EC%A0%9C%ED%92%80%EC%9D%B4-%EC%BD%94%ED%85%8C%EB%8C%80%EB%B9%84/dashboard ์ž๋ฐ”(Java) ์•Œ๊ณ ๋ฆฌ์ฆ˜ ๋ฌธ์ œํ’€์ด ์ž…๋ฌธ: ์ฝ”๋”ฉํ…Œ์ŠคํŠธ ๋Œ€๋น„ - ์ธํ”„๋Ÿฐ |

hyejin.tistory.com

-> ์ด์ „ ๋ฌธ์ œ 

 

 

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

์„ค๋ช…

๋Œ€๋ฌธ์ž์™€ ์†Œ๋ฌธ์ž๊ฐ€ ๊ฐ™์ด ์กด์žฌํ•˜๋Š” ๋ฌธ์ž์—ด์„ ์ž…๋ ฅ๋ฐ›์•„ ๋Œ€๋ฌธ์ž๋Š” ์†Œ๋ฌธ์ž๋กœ ์†Œ๋ฌธ์ž๋Š” ๋Œ€๋ฌธ์ž๋กœ ๋ณ€ํ™˜ํ•˜์—ฌ ์ถœ๋ ฅํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜์„ธ์š”.

 

์ž…๋ ฅ

์ฒซ ์ค„์— ๋ฌธ์ž์—ด์ด ์ž…๋ ฅ๋œ๋‹ค. ๋ฌธ์ž์—ด์˜ ๊ธธ์ด๋Š” 100์„ ๋„˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.

๋ฌธ์ž์—ด์€ ์˜์–ด ์•ŒํŒŒ๋ฒณ์œผ๋กœ๋งŒ ๊ตฌ์„ฑ๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค.

 

์ถœ๋ ฅ

์ฒซ ์ค„์— ๋Œ€๋ฌธ์ž๋Š” ์†Œ๋ฌธ์ž๋กœ, ์†Œ๋ฌธ์ž๋Š” ๋Œ€๋ฌธ์ž๋กœ ๋ณ€ํ™˜๋œ ๋ฌธ์ž์—ด์„ ์ถœ๋ ฅํ•ฉ๋‹ˆ๋‹ค.

 

์˜ˆ์‹œ ์ž…๋ ฅ 1 

StuDY

 

์˜ˆ์‹œ ์ถœ๋ ฅ 1

sTUdy

 

 

๋ฌธ์ œ ํ’€์ด 1 

public String solution(String input) {
    StringBuilder result = new StringBuilder();
    char[] chars = input.toCharArray();
    for (char aChar : chars) {
        if (aChar >= 'a' && aChar <= 'z') {
            result.append(Character.toUpperCase(aChar));
        }else {
            result.append(Character.toLowerCase(aChar));
        }
    }
    return result.toString();
}

๐Ÿ‘ฉ๐Ÿป‍๐Ÿ’ป : ๋‚˜๋Š” ์šฐ์„  ์ด๋ฒˆ ๋ฌธ์ œ์—์„œ๋„ toCharArray() ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•ด์„œ ๋ฌธ์ž ๋ฐฐ์—ด๋กœ ๋ณ€ํ™˜ํ•ด์คฌ๊ณ , ๊ทธ ๋‹ค์Œ ๋ฌธ์ž์—ด์„ ์šฐ์„  ์†Œ๋ฌธ์ž์ธ์ง€ ๋Œ€๋ฌธ์ž์ธ์ง€ ํŒŒ์•…ํ•œ ๋‹ค์Œ toUpperCase(), toLowerCase()๋ฅผ ํ†ตํ•ด ์†Œ๋ฌธ์ž๋Š” ๋Œ€๋ฌธ์ž๋กœ, ๋Œ€๋ฌธ์ž๋Š” ์†Œ๋ฌธ์ž๋กœ ๋ณ€ํ™˜ํ•ด์คฌ๋‹ค. 

๋น„๊ตํ•  ๋•Œ ์‚ฌ์šฉํ•œ ๋ฐฉ๋ฒ•์€ 'a' ๋ณด๋‹ค ํฌ๊ณ , 'z'๋ณด๋‹ค ์ž‘๋‹ค๋Š” ๊ฑด ์ง€๊ธˆ ์†Œ๋ฌธ์ž๋ผ๋Š” ๊ฑฐ๋‹ˆ๊นŒ, ํ•ด๋‹น ๋ฌธ์ž๋Š” ๋Œ€๋ฌธ์ž๋กœ ๋ณ€ํ™˜ํ•ด์ฃผ๊ณ  ๊ทธ ๋ฐ˜๋Œ€๋Š” ์†Œ๋ฌธ์ž๋กœ ๋ณ€ํ™˜ํ•ด์คฌ๋‹ค. 

 

 

 

๋ฌธ์ œ ํ’€์ด 2

public String solution2(String input) {
    StringBuilder answer = new StringBuilder();
    for (char c : input.toCharArray()) {
        if (Character.isLowerCase(c)) {
            answer.append(Character.toUpperCase(c));
        }else {
            answer.append(Character.toLowerCase(c));
        }
    }
    return answer.toString();
}

๐Ÿ‘พ : ๋‘๋ฒˆ์งธ ํ’€์ด ๋ฐฉ๋ฒ•์œผ๋กœ๋Š” isLowerCase() ๋ฅผ ํ†ตํ•ด์„œ ์†Œ๋ฌธ์ž์ด๋ฉด true ๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋Š” ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด๋‹ค. 

์ด ๋ฉ”์„œ๋“œ๊ฐ€ ์žˆ๋Š”์ง€ ๋ชฐ๋ž๋‹ค..!! true๋ฉด ๋Œ€๋ฌธ์ž๋กœ ๋ณ€ํ™˜ํ•˜๊ณ , false๋ฉด ์†Œ๋ฌธ์ž๋กœ ๋ณ€ํ™˜ํ•ด์ฃผ๋ฉด ๋œ๋‹ค. 

 

 

๋ฌธ์ œ ํ’€์ด 3

public String solution3(String input) {
    StringBuilder answer = new StringBuilder();
    for (char c : input.toCharArray()) {
        if (c >= 97 && c <= 122) answer.append((char) (c - 32));
        else answer.append((char) (c + 32));
    }
    return answer.toString();
}

๐Ÿ‘พ : ์„ธ๋ฒˆ์งธ ํ’€์ด ๋ฐฉ๋ฒ•์€ ์•„์Šคํ‚ค ๋„˜๋ฒ„๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•์ด๋‹ค. ์†Œ๋ฌธ์ž a ๋Š” 97 ์ด๊ณ  z๋Š” 122์ด๋‹ค. ์ฆ‰, ๋ฌธ์ž๊ฐ€ ์†Œ๋ฌธ์ž๋ผ๋ฉด ๊ทธ ๋ฌธ์ž์—์„œ 32๋ฅผ ๋นผ๋ฉด ๋Œ€๋ฌธ์ž๋กœ ๋ณ€ํ™˜๋œ๋‹ค.  (๋Œ€๋ฌธ์ž A๊ฐ€ 65, ์†Œ๋ฌธ์ž a๊ฐ€ 97) 

๋ฌด์กฐ๊ฑด toUpperCase, toLowerCase ์‚ฌ์šฉํ•  ์ƒ๊ฐ๋งŒ ํ–ˆ์—ˆ๋Š”๋ฐ..  32 ๋ฅผ ๋นผ์„œ ๊ฐ„๋‹จํ•˜๊ฒŒ ๋ณ€ํ™˜ํ•˜๋Š” ๋ฐฉ๋ฒ•๋„ ์žˆ๋‹ค๋Š” ๊ฑธ ์•Œ์•˜๋‹ค ! 

 

 

 

728x90