Kodekraftt
/Blog
/Capitalize First Letter Of Each Word In Java Definition Examples And Program
Join Early AccessContact UsPrivacy Policy
Java BasicsOOPsDSA with JavaQuizzesInterview Preparation

© 2026 KodeKraftt. All rights reserved.

Build smarter. Learn more. Innovate better.

Capitalize First Letter of Each Word in Java — Definition, Examples and Program

Learn how to capitalize the first letter of each word in a string in Java using character-level logic without using built-in string manipulation methods.

Capitalize First Letter of Each Word in Java

Capitalizing the first letter of each word is a common string manipulation task used in formatting names, titles, and sentences.

In this program, we process the string character by character without using built-in string formatting methods.

Understanding the Problem

Given a sentence, we need to:

  • Capitalize the first character of the sentence.
  • Capitalize any character that follows a space.
  • Keep all other characters unchanged.

Example 1

Input: java is fun

Output: Java Is Fun

Example 2

Input: welcome to kodekraftt

Output: Welcome To Kodekraftt

Logic Explanation

  1. Convert the string into a character array.
  2. Capitalize the first character if it is lowercase.
  3. Whenever a space is found, capitalize the next character.
  4. Append characters to form the final string.

Java Program to Capitalize First Letter of Each Word


import java.util.Scanner;

public class CapitalizeWords {

    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);

        System.out.print("Enter a sentence: ");
        String str = sc.nextLine();

        char[] ch = str.toCharArray();

        for (int i = 0; i < ch.length; i++) {

            if (i == 0 && ch[i] >= 'a' && ch[i] <= 'z') {
                ch[i] = (char)(ch[i] - 32);
            } 
            else if (ch[i] == ' ' && i + 1 < ch.length) {

                if (ch[i + 1] >= 'a' && ch[i + 1] <= 'z') {
                    ch[i + 1] = (char)(ch[i + 1] - 32);
                }
            }
        }

        System.out.print("Formatted string: ");
        System.out.println(ch);
    }
}

Sample Output


Enter a sentence: java is fun
Formatted string: Java Is Fun

Important Notes

  • Only lowercase letters are converted.
  • Multiple spaces are handled correctly.
  • No use of toUpperCase() or split().

Practice Challenges

  1. Convert the rest of each word to lowercase.
  2. Capitalize words separated by special characters.
  3. Ignore extra spaces at the beginning and end.

This program improves understanding of character manipulation and ASCII-based conversions in Java.

You might also like

Check if Two Strings Are Rotations of Each Other in Java — Definition, Examples and Program

Learn how to check whether two strings are rotations of each other in Java using logical string comparison without using complex libraries.

Find the Shortest Word in a Sentence in Java — Definition, Examples and Program

Learn how to find the shortest word in a sentence in Java using a simple character-by-character approach without using split() or advanced string methods.

Abundant Number in Java — Definition, Examples and Program

Learn what an abundant number is in Java with definition, examples, and a clean program to check whether a number is abundant or not.

Concatenate Two Strings in Java Without Using + Operator — Examples and Program

Learn how to concatenate two strings in Java without using the + operator. This blog explains the logic with examples, a Java program, and practice challenges.

Check if a String is Empty or Null in Java — Definition, Examples and Program

Learn how to check whether a string is null or empty in Java. This blog explains the difference, common mistakes, examples, and a safe Java program with practice challenges.

Convert String to Uppercase in Java Without Using String Library

Learn how to convert a string to uppercase in Java without using built-in String methods. This blog explains the ASCII-based logic with examples, a Java program, and practice challenges.