Capitalizing each word in a sentence with recursive function : Char Text « Development Class « Java






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

1.Capitalizing each word in a sentence with loop
2.Rules DemoRules Demo
3.Keys DemoKeys Demo
4.Break Iterator DemoBreak Iterator Demo
5.Character DemoCharacter Demo