-
-
Save digitalconceptvisuals/d0792c9d29cb710523620ca571f39447 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Evil { | |
public String changeCase(String caseType, String... words) { | |
// We support lower and camel cases | |
// Sanitize the user input | |
caseType = caseType | |
.trim() | |
.toLowerCase(); | |
// Accumulate output in StringBuilder | |
StringBuilder result = new StringBuilder(); | |
if (caseType.equals("lower")) { | |
// All words are converted to lower | |
// And then concatenated | |
for (String word : words) { | |
result.append( | |
word | |
.trim() | |
.toLowerCase() | |
); | |
} | |
} else if (caseType.equals("camel")) { | |
// First word is all lower | |
result.append( | |
words[0] | |
.trim() | |
.toLowerCase() | |
); | |
// Subsequent words are | |
// first -> upper | |
// rest -> lower | |
for (int offset = 1; | |
offset < words.length; | |
offset++) { | |
// First letter is upper | |
result.append( | |
words[offset] | |
.substring(0, 1) | |
.toUpperCase() | |
); | |
// Remaining letters are lower | |
result.append( | |
words[offset] | |
.substring(1) | |
.toLowerCase() | |
); | |
} | |
} | |
return result.toString(); | |
} | |
public static void main(String[] args) { | |
Evil obj = new Evil(); | |
String output = obj.changeCase("lower", "This", "Word"); | |
System.out.println(output); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment