[Programmers] JadenCase 문자열 만들기 - 연습문제

동민·2021년 3월 11일
// JadenCase 문자열 만들기 - 연습문제
public class JadenCase {

	public String solution(String s) {

		StringBuilder sb = new StringBuilder(s.toLowerCase());
		sb.replace(0, 1, (sb.charAt(0) + "").toUpperCase());
		for (int i = 1; i < sb.length(); i++) {
			if (sb.charAt(i) == ' ' && i < sb.length() - 1) { // "hello world  " 와 같이 뒤에 공백이 있을 때 ArrayIndexOutOfBounds 예외를 처리해주어야함
				sb.replace(i + 1, i + 2, (sb.charAt(i + 1) + "").toUpperCase());
			}
		}
		return sb.toString();
	}

	public static void main(String[] args) {

		JadenCase s = new JadenCase();
		System.out.println(s.solution("3people unFollowed me")); // 3people Unfollowed Me
		System.out.println(s.solution("for the last week")); // For The Last Week
		System.out.println(s.solution("hello  world ")); // 8번 Test Case 

	}

}
profile
BE Developer

0개의 댓글