Capitalize Words in a Sentence with Recursive Function

From Java Example Source Code

Jump to: navigation, search

Contents

[edit] Overview - Capitalize Words in a Sentence with Recursive Function

This is a example of Java program.

[edit] Java Source Code

  • Package: example.chartext1
  • File: Main.java
package example.chartext1;
 
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(" "));
	}
    }
}

[edit] What Result You Can Get

Run the program, you will get:


This Is A Test

[edit] Required External Libraries and/or Files for this Java Example

Need nothing.


[edit] How to Run this Java Example Program

We recommend running this Java example program with Eclipse.

For assistance in working with Eclipse, please see How to Run Java Program with Eclipse.

It's fairly easy.



[edit] Question & Answer

Any question?

Click edit and post your question or answer here.


Personal tools