Java Program to Capitalize the first character of each word in a String

In this program, we will see how to convert first letter of each word in a String to uppercase. For this purpose here we will use StringTokenizer class to break a given String into tokens and also we will use Java substring(int start_index, int end_index) method. Substring methods returns string from including starting index and excluding ending index.


Example:
Input: "hello how are you?"
Ouput: "Hello How Are You? "



Java Code:
import java.util.StringTokenizer;

public class Java {
public static void main(String[] args) {
String s = ("hello how are you?");
StringTokenizer st = new StringTokenizer(s, " ");
while (st.hasMoreTokens()) {
String name = st.nextToken();
System.out.print(name.substring(0, 1).toUpperCase() + name.substring(1) + " ");
}

}
}

Output:

Hello How Are You?


Also Read:
Reverse Words in a String.
Previous Post Next Post

Contact Form