This method converts a string to mixed case, id est mixedCase("hello, world!") -> "HeLlO, wOrLd!"
.
// Any input string is lower case already
private static String mixedCase(String input) {
// getAsCharacterList() simply turns a string into a List of Characters.
List<Character> charactersAsList = getAsCharacterList(input);
StringBuilder result = new StringBuilder();
int index = 0;
for (Character character : charactersAsList) {
if (Character.isLetter(character)) {
if (index % 2 == 0) {
result.append(Character.toUpperCase(character));
} else {
result.append(character);
}
index++;
} else {
result.append(character);
}
}
return result.toString();
}
It works, but admittedly it looks terrible and I have difficulties making it more concise. I would have preferred to use streams if I didn't had to reference indices...