class Solution {
public String solution(String s) {
StringBuilder answer = new StringBuilder();
// 처음 문자 처리
char firstChar = s.charAt(0);
answer.append(Character.isAlphabetic(firstChar) ? Character.toUpperCase(firstChar) : firstChar);
for(int i = 1; i < s.length(); i++){
//현재 문자가 공백인 경우
if(s.charAt(i) == ' ') {
answer.append(s.charAt(i));
continue;
}
//이전 문자가 공백인 경우
if(s.charAt(i-1) == ' ') {
answer.append(Character.toUpperCase(s.charAt(i)));
}else {
answer.append(Character.toLowerCase(s.charAt(i)));
}
}
return answer.toString();
}
}
첫 문자 처리에 대한 코드
그리고 연속된 공백에 대한 코드 처리를 하고
공백 뒤에 나오는 문자를 대문자 처리 아니면 소문자 처리하는 코드를 작성하면 된다!