Categories: Frequently Asked Java Programs In Interview

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:


                
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
=============================================================

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

Author: Amod Mahajan

My name is Amod Mahajan and I am an IT employee with 4+ years of experience in Software testing and staying in Bengaluru. My area of interest is Automation testing. I started from basics and went throw so many selenium tutorials. Thanks to Mukesh Otwani as his tutorials are easy and cover basics to advance. I have habit of exploring concepts by deep diving. I used to make notes. I thought of sharing my knowledge through posts and now I am here. #KeepLearning #ShareLearning




              
Amod Mahajan

My name is Amod Mahajan and I am an IT employee with 4+ years of experience in Software testing and staying in Bengaluru. My area of interest is Automation testing. I started from basics and went throw so many selenium tutorials. Thanks to Mukesh Otwani as his tutorials are easy and cover basics to advance. I have habit of exploring concepts by deep diving. I used to make notes. I thought of sharing my knowledge through posts and now I am here. #KeepLearning #ShareLearning

Recent Posts

TestNG Tutorials 64: Dependency in TestNG – ignoreMissingDependencies – Another Way of Achieving Soft Dependencies

Hello Folks, There are two types of dependencies in TestNG: 1. Hard Dependency : All the methods you depend on must have…

19 hours ago

API Testing Tutorial Part 14 – Installation of Postman Tool

Hello Folks, As part of our API Testing series, we will see “Installation of Postman Tool” in this post. Postman tool was…

19 hours ago

TestNG Tutorials 63: Dependency in TestNG – Usage of Regular Expressions with DependsOnGroup

Hello Folks, We have learnt in previous posts regarding establishing relationship between test methods. You can go through them below:…

6 days ago

Frequently Asked Java Program 25: Java Program to Convert a String Sentence in Camel Case

"CamelCase" is a naming convention in which words are joined together without any whitespace in between and each word starts…

7 days ago

Ways of Handling StaleElementReferenceException Without PageFactory

Hello Folks, Previously, I had published on Handling StaleElementReferenceException using PageFactory. But many people asked how to handle it if they…

2 weeks ago

How to Verify if Options in Dropdown are Sorted as Expected In Selenium WebDriver – Java

Hello Guys, As I say always, your automation script is ineffective if you do not include logic to validate to…

3 weeks ago