Java Programs›Strings›Capitalize First Letter of Each Word

Capitalize First Letter of Each Word in Java

beginner·  Strings  ·  String

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.

Input
" learn java one program at a time "
Output
"Learn Java One Program At A Time"

Java Program

Java
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

Learn Java One Program At A Time

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.

How It Works
  1. 1trim() removes spaces at both ends, and split("\\s+") splits on any run of whitespace, so no empty words appear.
  2. 2For each word, Character.toUpperCase(word.charAt(0)) capitalizes the first letter.
  3. 3word.substring(1).toLowerCase() normalises the rest, turning jAVA into Java.
  4. 4A space is added before every word except the first, so the result has single spaces only.
The leading spaces and the three spaces after 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.

Complexity
Time Complexity: O(n)Space Complexity: O(n)

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

trim()split() with regexCharacter.toUpperCase()StringBuilder

Approach 2: Java 8

Java
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

Learn Java One Program At A Time

Core Logic

The word-by-word transformation maps naturally onto a stream: split, map each word, then join with single spaces.

How It Works
  1. 1Arrays.stream(...split("\\s+")) streams the words.
  2. 2map rebuilds each word with an uppercase first letter and lowercase rest.
  3. 3Collectors.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.

Complexity
Time Complexity: O(n)Space Complexity: O(n)

Why: The same per-character work happens, and the stream builds a new string of the same length.

Key Concepts

Arrays.stream()map()Collectors.joining()

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.

Learn the Concept

Related Programs