본문 바로가기
프로그래머스/연습문제 Level2

프로그래머스 연습문제 LV2 - JadenCase 문자열 만들기

by 맴썰 2021. 9. 14.

문제 설명

JadenCase란 모든 단어의 첫 문자가 대문자이고, 그 외의 알파벳은 소문자인 문자열입니다. 문자열 s가 주어졌을 때, s를 JadenCase로 바꾼 문자열을 리턴하는 함수, solution을 완성해주세요.

제한 조건

  • s는 길이 1 이상인 문자열입니다.
  • s는 알파벳과 공백문자(" ")로 이루어져 있습니다.
  • 첫 문자가 영문이 아닐때에는 이어지는 영문은 소문자로 씁니다. ( 첫번째 입출력 예 참고 )

입출력 예

s                                                                            return

"3people unFollowed me" "3people Unfollowed Me"
"for the last week" "For The Last Week"

import java.util.*;
class Solution {
    public String solution(String s) {
        String answer = "";
        String[] temp = s.split(" ");
        String a =" ";
        for(int i=0; i<temp.length;i++){
            if(temp[i].equals("")) continue;
            char t1 = temp[i].charAt(0);
            String t2 = temp[i].substring(1);
            t2 = t2.toLowerCase();
            a = String.valueOf(t1).toUpperCase();
            temp[i] = a.concat(t2);
        }
        for(int i=0; i<temp.length;i++){
            answer = answer.concat(temp[i]);
            if(i==temp.length-1) break;
            answer = answer.concat(" ");
        }
        if(s.charAt(s.length()-1)==' ') answer = answer.concat(" ");
        return answer;
    }
}