Frequently Asked Java Program 24: Java Program to Capitalize First Character of Each Word in a String Sentence

Problem Statement:

Input:

make selenium easy

Output:

Make Selenium Easy

Convert first character of each word into upper case.

Solution:

Logic:

  1. A sentence is a collection of words generally separated by a white space. We can extract words of given sentence using split method.
  2.  Now extract first character of first word and check if it is not uppercase already. If it is not, convert first char to upper case and append remaining characters of word. we can use subString method. If it is already in uppercase, append entire word. AT last append a whitespace. Repeat the same for all words.

Java Program:

package StringPrograms;

import java.util.Scanner;

public class CapitaliseFirstCharOfWords {

	public static String CapitaliseCharOfWords(String sentence) {

		// Extract all words
		String words[] = sentence.split("\\s+");
		
		// Creating an empty string of type StringBuilder so that modification of string is possible.
		StringBuilder sb = new StringBuilder();
		
		// Iterating through words
		for (String word : words) {
			//Extracting first char
			char firstChar = word.charAt(0);
			// Checking if firstchar is not in upper case already
			if (!Character.isUpperCase(firstChar)) {
				// Convert first char into upper case and then append remaining characters of words. 
				sb.append(Character.toUpperCase(firstChar)).append(word.substring(1));
			} else
				sb.append(word.substring(0));
			
			// Appending space after each word
			sb.append(" ");
		}
		
		// Converting StringBuilder to String. trim() is needed to trim last space appended. 
		String result = sb.toString().trim();
		return result;
	}

	public static void main(String[] args) {

		// User input for the string to know length
		Scanner sc = new Scanner(System.in);
		System.out.println("Please enter the string to Capitalize first char of each word:");

		String userInput = sc.nextLine();

		sc.close();

		System.out.println("You entered: " + userInput);

		System.out.println("Output is :" + CapitaliseCharOfWords(userInput));

	}

}

 

Output:

[java]
Please enter the string to Capitalize first char of each word:
make selenium easy
You entered: make selenium easy
Output is :Make Selenium Easy
=============================================================
Please enter the string to Capitalize first char of each word:
Make Selenium Easy
You entered: Make Selenium Easy
Output is :Make Selenium Easy
=============================================================
Please enter the string to Capitalize first char of each word:
make selenium easy 123
You entered: make selenium easy 123
Output is :Make Selenium Easy 123
=============================================================
Please enter the string to Capitalize first char of each word:
make selenium easy &special
You entered: make selenium easy &special
Output is :Make Selenium Easy &special
=============================================================
Please enter the string to Capitalize first char of each word:
selenium
You entered: selenium
Output is :Selenium
=============================================================
[/java]

Note- A Java library named Apache Commons provides a class "WordUtils" provides method to achieve this. But in interview, it is expected to solve with logic instead of using built in methods.

#HappyCoding

Leave a Reply

Your email address will not be published. Required fields are marked *