CamelCase Method

Instructions

Write simple .camelCase method (camel_case function in PHP, CamelCase in C# or camelCase in Java) for strings. All words must have their first letter capitalized without spaces.

For instance:

1
2
camelCase("hello case"); // => "HelloCase"
camelCase("camel case word"); // => "CamelCaseWord"

My Solution

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
import java.util.Arrays;
import java.util.stream.Collectors;

public class CamelCaseMethod {
public static String camelCase(String str) {
String camelCaseStr = Arrays.stream(str.trim().split(" "))
.map(CamelCaseMethod::capitalize)
.collect(Collectors.joining());

return camelCaseStr;
}

public static String capitalize(String word) {
if (word.length() == 0)
return word;

return Character.toUpperCase(word.charAt(0)) + word.substring(1);
}

}

Better Solution

그다지 없었다…

Feeling

문제는 어렵지 않았는데 테스트 케이스에 아래와 같은 케이스가 포함되있어서 풀기가 은근히 까다로웠다.

1
2
3
"ab  c"
" camel case word"
"say hello "

많이 풀어보면서 이런 케이스에 시간 뺐기지 않도록 하자

Share