Will Smith's son Jaden Smith is a movie star, such as karate kid (2010) and earth after (2013). Jaden is known for some of the philosophy he provides through Twitter. When writing on twitter, he almost always capitalizes every word.
Your task is to convert the string to the way Jaden Smith was written. These strings are actual references from Jaden Smith, but they are not capitalized as he originally entered. (capitalize each word)
Example:
Not in Jaden's style: "How can mirrors be real if our eyes aren't real" Jaden's style: "How Can Mirrors Be Real If Our Eyes Aren't Real"
Note that the Java version requires a null or null return value for an empty string.
- My initial solution
public class JadenCase { public String toJadenCase(String phrase) { // TODO put your code below this comment if (phrase == null || "".equals(phrase)) { return null; } String list[] = phrase.split(" "); String juzi = ""; if (list != null && list.length > 0) { for (int i = 0; i < list.length; i++) { String uppWord = list[i].toUpperCase(); char first = uppWord.charAt(0); String one = first + list[i].substring(1, list[i].length()) + " "; juzi += one; } return juzi.substring(0,juzi.length()-1); } return null; } }
The best solution to the top three voting
import java.lang.Character; public class JadenCase { public String toJadenCase(String phrase) { if(phrase == null || phrase.equals("")) return null; char[] array = phrase.toCharArray(); for(int x = 0; x < array.length; x++) { if(x == 0 || array[x-1] == ' ') { array[x] = Character.toUpperCase(array[x]); } } return new String(array); } }
2.
import java.util.Arrays; import java.util.stream.Collectors; public class JadenCase { public String toJadenCase(String phrase) { if (null == phrase || phrase.length() == 0) { return null; } return Arrays.stream(phrase.split(" ")) .map(i -> i.substring(0, 1).toUpperCase() + i.substring(1, i.length())) .collect(Collectors.joining(" ")); } }
3.
import java.util.Arrays; import java.util.stream.Collectors; public class JadenCase { public String toJadenCase(String phrase) { if(phrase == null || phrase.isEmpty()) return null; return Arrays.stream(phrase.split("\\s+")).map(str -> Character.toUpperCase(str.charAt(0)) + str.substring(1)) .collect(Collectors.joining(" ")); } }
Reference link:
Solutions: Jaden Casing Strings