본문으로 바로가기
728x90
반응형

문제

 

 


풀이

 

 

반응형

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
class Solution {
    public String solution(String s) {
       String answer = "";
       int index = 0;                                        //가상의 index 저장용
        for(int i=0; i<s.length(); i++) {
            if(s.charAt(i)==32) {                            //i번째 문자가 공백일경우
                answer+=" ";                                 //answer에 공백을 더함          
                index = 0;                                   //공백이 나왔으니 index는 다시 0으로 맞춤
            }else if(index==0 || index%2==0) {               //index가 0 이거나 짝수이면
                answer+=Character.toUpperCase(s.charAt(i));  //대문자를 더하고
                index++;                                     //index +1
            }else if(index%2!=0) {                           //index가 홀수이면
                answer+=Character.toLowerCase(s.charAt(i));  //소문자를 더하고
                index++;                                     //index +1
            }
        }
        return answer;
    }
}
cs

 

728x90
반응형