Capitalize First Letter of Each Word in Java
Problem
Title case writes every word with an uppercase first letter and lowercase remaining letters, as in book and article titles.
Given a sentence with irregular spacing, capitalize the first letter of every word and join the words with single spaces.
Java Program
public class CapitalizeEachWord {
public static void main(String[] args) {
String sentence = " learn java one program at a time ";
String[] words = sentence.trim().split("\\s+"); // trim + \\s+ ignores extra spaces
StringBuilder result = new StringBuilder();
for (String word : words) {
if (result.length() > 0) result.append(' ');
result.append(Character.toUpperCase(word.charAt(0))) // first letter up
.append(word.substring(1).toLowerCase()); // the rest down
}
System.out.println(result);
}
}Output
Core Logic
Splitting the sentence into words lets each word be rebuilt separately — first letter upper-cased, the rest lower-cased — and then joined back together.
- 1
trim()removes spaces at both ends, andsplit("\\s+")splits on any run of whitespace, so no empty words appear. - 2For each word,
Character.toUpperCase(word.charAt(0))capitalizes the first letter. - 3
word.substring(1).toLowerCase()normalises the rest, turningjAVAintoJava. - 4A space is added before every word except the first, so the result has single spaces only.
java disappear, and each of the seven words gets an uppercase first letter.Key Point: Splitting on " " instead of "\\s+" would create empty strings for repeated spaces, and charAt(0) on an empty string throws an exception.
Why: Each character is visited a constant number of times while splitting and rebuilding, and the words and result take space proportional to the input.
Key Concepts
Approach 2: Java 8
import java.util.Arrays;
import java.util.stream.Collectors;
public class CapitalizeEachWordStream {
public static void main(String[] args) {
String sentence = " learn java one program at a time ";
String result = Arrays.stream(sentence.trim().split("\\s+"))
.map(w -> Character.toUpperCase(w.charAt(0)) + w.substring(1).toLowerCase())
.collect(Collectors.joining(" "));
System.out.println(result);
}
}
Output
Core Logic
The word-by-word transformation maps naturally onto a stream: split, map each word, then join with single spaces.
- 1
Arrays.stream(...split("\\s+"))streams the words. - 2
maprebuilds each word with an uppercase first letter and lowercase rest. - 3
Collectors.joining(" ")glues the words back together with one space between each.
"program" maps to "Program", and joining all seven mapped words gives the final sentence.Key Point: joining handles the separators for you, so there is no need for the "is this the first word?" check from the loop version.
Why: The same per-character work happens, and the stream builds a new string of the same length.
Key Concepts
Interview Questions
How do you handle words that are already in uppercase?
Lower-case the rest of each word after capitalising its first letter. Without that step, "jAVA" would become "JAVA" instead of "Java".
Does Java have a built-in title-case method?
No. The JDK has toUpperCase() and toLowerCase() for whole strings, but capitalising each word needs your own loop or stream, or a third-party library.