프로그래머스에서 본 예제를 자바로 구현해봤습니다..
1. 문제
Jaden_Case함수는 문자열 s을 매개변수로 입력받습니다.
s에 모든 단어의 첫 알파벳이 대문자이고, 그 외의 알파벳은 소문자인 문자열을 리턴하도록 함수를 완성하세요
예를들어 s가 3people unFollowed me for the last week
라면 3people Unfollowed Me For The Last Week
를 리턴하면 됩니다.
2. 코드
package Test; public class JadenCase { public static void main(String[] args) { String s = "3people unFollowed me for the last week"; JadenCase jc = new JadenCase(); } private String jadenCase(String s) { boolean isNextUpper = true; StringBuffer sb = new StringBuffer(); for ( int inx=0; inx<s.length(); inx++ ) { char c = s.charAt(inx); if ( inx == 0 || isNextUpper ) { sb.append(Character.toUpperCase(c)); isNextUpper = false; } else { sb.append(Character.toLowerCase(c)); } if ( c == ' ') { isNextUpper = true; } } return sb.toString(); } }