자바 - 이상한 문자 만들기

백종석·2022년 5월 16일
0
post-thumbnail

문제 :

풀이 :

class Solution {
    public String solution(String s) {
        String answer = "";
        // 문자열 s를 한글자씩 word에 담는 배열 선언
        String[] word = s.split("");
        
        // word 배열의 순번 index
        int index = 0;
        
        // i가 배열 word의 길이만큼 반복하며 증가할 때
        for(int i=0; i<word.length; i++){
        	// word[i]번째 단어가 공백일 경우 index = 0으로 설정
            if(word[i].equals(" ")) {
                index = 0;
            // 만약 index가 짝수이면 
            }else if(index % 2 == 0 ){
            	// 짝수번째 word[i]번째 알파벳을 대문자로 변환
                word[i] = word[i].toUpperCase();
                // index 1 증가
                index ++;
            // index가 홀수이면
            }else if(index % 2 != 0){
            	//	word[i]번째 알파벳 소문자로 전환
                word[i] = word[i].toLowerCase();
                index ++;
            }
            // answer에 알파벳을 하나씩 추가
            answer += word[i];
            // System.out.println(answer);
            // 출력 결과
            // T
            // Tr
            // TrY
            // TrY 
            // TrY H
            // TrY He
            // TrY HeL
            // TrY HeLl
            // TrY HeLlO
            // TrY HeLlO 
            // TrY HeLlO W
            // TrY HeLlO Wo
            // TrY HeLlO WoR
            // TrY HeLlO WoRl
            // TrY HeLlO WoRlD = answer
        }
        return answer;
    }
}
profile
항해중인 우당탕탕 코린이

0개의 댓글