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(" "));
}
}
}
Related examples in the same category