View

๋ฐ˜์‘ํ˜•

์ด์ƒํ•œ ๋ฌธ์ž ๋งŒ๋“ค๊ธฐ

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


๋ฌธ์ œ ์„ค๋ช…

๋ฌธ์ž์—ด s๋Š” ํ•œ ๊ฐœ ์ด์ƒ์˜ ๋‹จ์–ด๋กœ ๊ตฌ์„ฑ๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค. ๊ฐ ๋‹จ์–ด๋Š” ํ•˜๋‚˜ ์ด์ƒ์˜ ๊ณต๋ฐฑ๋ฌธ์ž๋กœ ๊ตฌ๋ถ„๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค. ๊ฐ ๋‹จ์–ด์˜ ์ง์ˆ˜๋ฒˆ์งธ ์•ŒํŒŒ๋ฒณ์€ ๋Œ€๋ฌธ์ž๋กœ, ํ™€์ˆ˜๋ฒˆ์งธ ์•ŒํŒŒ๋ฒณ์€ ์†Œ๋ฌธ์ž๋กœ ๋ฐ”๊พผ ๋ฌธ์ž์—ด์„ ๋ฆฌํ„ดํ•˜๋Š” ํ•จ์ˆ˜, solution์„ ์™„์„ฑํ•˜์„ธ์š”.

์ œํ•œ ์‚ฌํ•ญ

  • ๋ฌธ์ž์—ด ์ „์ฒด์˜ ์ง/ํ™€์ˆ˜ ์ธ๋ฑ์Šค๊ฐ€ ์•„๋‹ˆ๋ผ, ๋‹จ์–ด(๊ณต๋ฐฑ์„ ๊ธฐ์ค€)๋ณ„๋กœ ์ง/ํ™€์ˆ˜ ์ธ๋ฑ์Šค๋ฅผ ํŒ๋‹จํ•ด์•ผํ•ฉ๋‹ˆ๋‹ค.
  • ์ฒซ ๋ฒˆ์งธ ๊ธ€์ž๋Š” 0๋ฒˆ์งธ ์ธ๋ฑ์Šค๋กœ ๋ณด์•„ ์ง์ˆ˜๋ฒˆ์งธ ์•ŒํŒŒ๋ฒณ์œผ๋กœ ์ฒ˜๋ฆฌํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

์ž…์ถœ๋ ฅ ์˜ˆ

s return

"try hello world" "TrY HeLlO WoRlD"

์ž…์ถœ๋ ฅ ์˜ˆ ์„ค๋ช…

"try hello world"๋Š” ์„ธ ๋‹จ์–ด "try", "hello", "world"๋กœ ๊ตฌ์„ฑ๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค. ๊ฐ ๋‹จ์–ด์˜ ์ง์ˆ˜๋ฒˆ์งธ ๋ฌธ์ž๋ฅผ ๋Œ€๋ฌธ์ž๋กœ, ํ™€์ˆ˜๋ฒˆ์งธ ๋ฌธ์ž๋ฅผ ์†Œ๋ฌธ์ž๋กœ ๋ฐ”๊พธ๋ฉด "TrY", "HeLlO", "WoRlD"์ž…๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ "TrY HeLlO WoRlD" ๋ฅผ ๋ฆฌํ„ดํ•ฉ๋‹ˆ๋‹ค.


<aside> ๐Ÿ’ก ๋ฌธ์ž์—ด์„ ๋Œ€์†Œ๋ฌธ์ž๋กœ ๋ณ€๊ฒฝ

</aside>

***๐Ÿ“ ๋ฌธ์ž์—ด(String)์„ ๋Œ€์†Œ๋ฌธ์ž๋กœ ๋ณ€๊ฒฝ*** 

String s = "HI";
s = s.toUpperCase();

***๐Ÿ“ ๋ฌธ์ž(char)๋ฅผ ๋Œ€์†Œ๋ฌธ์ž๋กœ ๋ณ€๊ฒฝ***
char c = 'H';
c = Character.toLowerCase(c);

***๐Ÿ“ ๋ฌธ์ž(char)๋ฅผ ๋ฌธ์ž์—ด(String)๋กœ ๋ณ€ํ™˜***

char arr []= {'a','b','c','d','e'};
 String arr2 = String.valueOf(arr);   

import java.util.*;

class Solution {
    public String solution(String s) {
        char arr [] = new char [s.length()];
        boolean check = true; // ์ง์ˆ˜: true, ํ™€์ˆ˜: false
        
        for (int i=0; i<s.length(); i++){
            
            // ๊ณต๋ฐฑ์ž„?
            if(s.charAt(i)==' ') {
                arr[i] = ' ';
                check=true;
            }
            
            else if(check) // ์ง์ˆ˜๋ฉด    
                
            {
                arr[i] = Character.toUpperCase(s.charAt(i));
                check = false;
            }
            
            else //ํ™€์ˆ˜๋ฉด
            {
                arr[i] = Character.toLowerCase(s.charAt(i));
                check = true;
            }            
        }
                        
        String answer = String.valueOf(arr);        
        return answer;
    }
}
๋ฐ˜์‘ํ˜•
Share Link
reply
๋ฐ˜์‘ํ˜•
ยซ   2024/11   ยป
์ผ ์›” ํ™” ์ˆ˜ ๋ชฉ ๊ธˆ ํ† 
1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30