Capitalizing each word in a sentence with recursive function
public class Main { public static void main(String[] args) { String sentence = "this is a test"; System.out.println(capSentence(sentence, true)); } public static String capSentence(String string, boolean capitalize) { if (string.length() == 0) { return ""; } String c = string.substring(0, 1); if (capitalize) { return c.toUpperCase() + capSentence(string.substring(1), c.equals(" ")); } else { return c.toLowerCase() + capSentence(string.substring(1), c.equals(" ")); } } }
1. | Capitalizing each word in a sentence with loop | ||
2. | Rules Demo | ||
3. | Keys Demo | ||
4. | Break Iterator Demo | ||
5. | Character Demo |