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.
- How to Convert First Letter of String to Uppercase in Java?
- Program to Capitalize First Letter of Each Word in Java
- Write a Program to Change the First Letter in Every Word of the String into Uppercase?
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:
- How to Convert a String into Lowercase in Java
- How to remove Leading and Trailing Whitespaces from String in Java?
- How to compare two Strings using equalsIgnoreCase() Method in Java?
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:
- python capitalize the first letter of each word in a string
- convert first letter of each word of a string to upper case in cpp
- python program to read a file and capitalize the first letter of every word in the file
- how to uppercase the first letter of a string in javascript
- check the first or last character of a string in python
- find frequency of each character in string and their indices and finding duplicate characters in a string
- find all occurrences of a sub string in a string cpp both case sensitive insensitive