🍭 프로그래머스 12951번 – JadenCase 문자열 만들기
주어진 문자열 s를 JadenCase 형태로 바꾸려 합니다.
' ') 하나로 구분하며, 공백이 연속해서 나올 수도 있습니다. 제한 조건
s의 길이는 1 이상 200 이하입니다. s는 알파벳, 숫자, 공백으로 이루어져 있습니다. // 입력
"3people unFollowed me"
// 출력
"3people Unfollowed Me"
public class Solution {
public static void main(String[] args) {
System.out.println(solution("3people unFollowed me"));
}
public static String solution(String s) {
int idx = 0;
StringBuilder sb = new StringBuilder();
for (int i = 0; i < s.length(); i++) {
char c = s.charAt(i);
// 공백이면 다음 글자를 단어 첫 글자로 만들기 위해 idx 초기화
if (c == ' ') {
sb.append(c);
idx = -1;
}
// 숫자는 변환 없이 그대로 추가
else if (Character.isDigit(c)) {
sb.append(c);
}
// 첫 글자면 대문자, 아니면 소문자
else if (idx == 0) {
sb.append(Character.toUpperCase(c));
} else {
sb.append(Character.toLowerCase(c));
}
idx++;
}
return sb.toString();
}
}
Character 클래스의 isDigit, toUpperCase, toLowerCase 메서드를 활용해 문자 타입별로 깔끔하게 처리 가능.