Java capitalize first letter of each word – Capitalize First Letter of each Word in a String in Java | Camel Case Naming Conventions in Java

Java capitalize first letter of each word: Strings, which are commonly used in Java programming, are a series of characters. Strings are considered objects in the Java programming language. To create and manipulate strings, the Java platform provides the String class. Given a string, the task is to capitalize the first letter of each word in a string. This tutorial will make you acquainted with the details such as How to Convert First Letter of String to Uppercase in Java, Example Program to Capitalize First Letter of Each Word in Java.

Example:

Input:

given_string="hello this is btechGeeks"

Output:

Hello This Is BTechGeeks

How to Convert First Letter of String to Uppercase in Java?

Approach:

  • Iterate through the string from start to finish.
  • Check for white space or not for each character.
  • Update a true IS SPACE flag if it is a white space.
  • If the character between ‘a’ and ‘z’ is true and the flag is SPACE, this character should be converted to upper case.

Read Similar Articles:

Program to Capitalize First Letter of Each Word in Java

import java.io.*;
import java.lang.*;
import java.util.*;

class BtechGeeks {
    public static void main(String[] args)
        throws java.lang.Exception
    { // given_string
        String given_string = "Hello this is BTechGeeks";
        // Taking a StringBuilder and convert this string to
        // StringBuilder
        StringBuilder given_string_builder
            = new StringBuilder(given_string);
        // To keep track of whether the last visited
        // character was a white space or not, set a flag.
        boolean ispreviousSpace = true;
        int i;
        // Traverse the string
        for (i = 0; i < given_string_builder.length();
             i++) {
            char character = given_string_builder.charAt(i);

            if (ispreviousSpace && character >= 'a'
                && character <= 'z') {
                // The character must be converted to
                // uppercase.
                given_string_builder.setCharAt(
                    i, (char)(character + ('A' - 'a')));
                ispreviousSpace = false;
            }
            else if (character != ' ')
                ispreviousSpace = false;
            else
                ispreviousSpace = true;
        }
        // converting string builder to string and printing
        // it
        System.out.println(given_string_builder.toString());
    }
}

Output:

Hello This Is BTechGeeks

Write a Program to Change the First Letter in Every Word of the String into Uppercase?

// Java program to convert first character
// uppercase in a sentence
class GFG {

static String convert(String str)
{

// Create a char array of given String
char ch[] = str.toCharArray();
for (int i = 0; i < str.length(); i++) {

// If first character of a word is found
if (i == 0 && ch[i] != ' ' ||
ch[i] != ' ' && ch[i - 1] == ' ') {

// If it is in lower-case
if (ch[i] >= 'a' && ch[i] <= 'z') {

// Convert into Upper-case
ch[i] = (char)(ch[i] - 'a' + 'A');
}
}

// If apart from first character
// Any one is in Upper-case
else if (ch[i] >= 'A' && ch[i] <= 'Z')

// Convert into Lower-Case
ch[i] = (char)(ch[i] + 'a' - 'A'); 
}

// Convert the char array to equivalent String
String st = new String(ch);
return st;
}

public static void main(String[] args)
{
String str = "bTechGeeks";
System.out.println(convert(str));
}
}

Output:

BTechGeeks

Related Programs: