0
0
JavaProgramBeginner · 2 min read

Java Program to Convert String to Title Case

Use Java to convert a string to title case by splitting the string into words, capitalizing the first letter of each word with 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

Inputhello world
OutputHello World
Inputjava PROGRAMMING language
OutputJava Programming Language
Input multiple spaces here
OutputMultiple Spaces Here
🧠

How to Think About It

To convert a string to title case, first split the string into individual words by spaces. Then, for each word, change the first letter to uppercase and the rest to lowercase. Finally, join all the words back together with spaces to form the title-cased string.
📐

Algorithm

1
Get the input string.
2
Split the string into words using space as a separator.
3
For each word, convert the first character to uppercase and the rest to lowercase.
4
Join all the processed words back into a single string separated by spaces.
5
Return or print the resulting title case string.
💻

Code

java
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);
    }
}
Output
Java Programming Language
🔍

Dry Run

Let's trace the input "java PROGRAMMING language" through the code

1

Trim and split input

Input string trimmed: "java PROGRAMMING language"; split into words: ["java", "PROGRAMMING", "language"]

2

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"

3

Join words

Join processed words with spaces → "Java Programming Language"

WordFirst Char UppercaseRest LowercaseResult
javaJavaJava
PROGRAMMINGProgrammingProgramming
languageLanguageLanguage
💡

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

Using StringBuilder and manual loop
java
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"));
    }
}
This approach uses a simple loop and StringBuilder, which is easy to understand and efficient for beginners.
Using Apache Commons Lang WordUtils
java
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);
    }
}
This method uses a library to handle title casing, which is very concise but requires adding an external dependency.

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.

ApproachTimeSpaceBest For
Stream API with mapO(n)O(n)Readable and concise code
Manual loop with StringBuilderO(n)O(n)Performance and simplicity
Apache Commons WordUtilsO(n)O(n)Quick solution with external library
💡
Always trim the input and split by one or more spaces to handle extra spaces correctly.
⚠️
Beginners often forget to convert the rest of the word to lowercase, resulting in inconsistent casing.