Java Program to Convert String to Title Case
Character.toUpperCase(), and joining them back; for example, String titleCase = Arrays.stream(input.split(" ")).map(word -> Character.toUpperCase(word.charAt(0)) + word.substring(1).toLowerCase()).collect(Collectors.joining(" "));.Examples
How to Think About It
Algorithm
Code
import java.util.Arrays; import java.util.stream.Collectors; public class TitleCaseConverter { public static String toTitleCase(String input) { return Arrays.stream(input.trim().split("\\s+")) .map(word -> word.isEmpty() ? word : Character.toUpperCase(word.charAt(0)) + word.substring(1).toLowerCase()) .collect(Collectors.joining(" ")); } public static void main(String[] args) { String input = "java PROGRAMMING language"; String titleCase = toTitleCase(input); System.out.println(titleCase); } }
Dry Run
Let's trace the input "java PROGRAMMING language" through the code
Trim and split input
Input string trimmed: "java PROGRAMMING language"; split into words: ["java", "PROGRAMMING", "language"]
Process each word
For "java": first char 'j' to uppercase 'J', rest "ava" to lowercase "ava" → "Java" For "PROGRAMMING": first char 'P' uppercase 'P', rest "ROGRAMMING" lowercase "rogramming" → "Programming" For "language": first char 'l' uppercase 'L', rest "anguage" lowercase "anguage" → "Language"
Join words
Join processed words with spaces → "Java Programming Language"
| Word | First Char Uppercase | Rest Lowercase | Result |
|---|---|---|---|
| java | J | ava | Java |
| PROGRAMMING | P | rogramming | Programming |
| language | L | anguage | Language |
Why This Works
Step 1: Splitting the string
We split the input string by spaces using split("\\s+") to get each word separately.
Step 2: Capitalizing each word
For each word, we use Character.toUpperCase() on the first letter and toLowerCase() on the rest to ensure proper title case.
Step 3: Joining words back
We join all the processed words with spaces using Collectors.joining(" ") to form the final title case string.
Alternative Approaches
public class TitleCaseManual { public static String toTitleCase(String input) { String[] words = input.trim().split("\\s+"); StringBuilder titleCase = new StringBuilder(); for (String word : words) { if (word.length() > 0) { titleCase.append(Character.toUpperCase(word.charAt(0))) .append(word.substring(1).toLowerCase()) .append(" "); } } return titleCase.toString().trim(); } public static void main(String[] args) { System.out.println(toTitleCase("hello world")); } }
import org.apache.commons.lang3.text.WordUtils; public class TitleCaseApache { public static void main(String[] args) { String input = "java PROGRAMMING language"; String titleCase = WordUtils.capitalizeFully(input); System.out.println(titleCase); } }
Complexity: O(n) time, O(n) space
Time Complexity
The program processes each character once when splitting and converting words, so time complexity is O(n), where n is the length of the input string.
Space Complexity
Extra space is used to store the split words and the output string, so space complexity is O(n).
Which Approach is Fastest?
The manual loop with StringBuilder is generally fastest and uses minimal overhead, while stream-based and library methods are more readable but may have slight overhead.
| Approach | Time | Space | Best For |
|---|---|---|---|
| Stream API with map | O(n) | O(n) | Readable and concise code |
| Manual loop with StringBuilder | O(n) | O(n) | Performance and simplicity |
| Apache Commons WordUtils | O(n) | O(n) | Quick solution with external library |