Skip to content

Make Selenium Easy

And Keep It That Way

  • Home
  • Share
  • Toggle search form

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

Posted on 03/21/2025 By admin

make selenium easy

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
Uncategorized

Post navigation

Previous Post: TestNG Tutorials 49: Need of DataProvider Method in TestNG
Next Post: How Page Factory In Selenium WebDriver Performs Lazy Initialization

Related Posts

Cucumber Tutorial 2 – Setup a Basic Maven-Cucumber 5 -TestNG Project in Eclipse Uncategorized
October 4, 2017 – Make Selenium Easy Uncategorized
SelectTestngXMl – Make Selenium Easy Uncategorized
Why And How Should We Terminate Browser Driver Executable File In Selenium WebDriver Uncategorized
Git Tutorial 26 – How To Ignore Already Indexed/staged/Committed/Pushed File to Git Repo Uncategorized
How to handle StaleElementReferenceException Through Page Object Model – PageFactory – Make Selenium Easy Uncategorized

Recent Posts

  • Getting Started with Selenium 4: What Is New and How to Upgrade from Selenium 3
  • Manual Testing
  • Baby Steps To Become Efficient Selenium-Java Automation Tester
  • Features of Selenium 4.0.0 Release – Java Binding
  • Part 1: Handling Drop-down Created Using SELECT Tag In Selenium

Recent Comments

No comments to show.

Archives

  • April 2026
  • April 2025
  • March 2025
  • February 2025
  • January 2025
  • December 2024
  • November 2024
  • October 2024
  • September 2024
  • August 2024
  • April 2024
  • March 2024
  • February 2024
  • December 2023
  • October 2023
  • August 2023
  • November 2022
  • September 2022
  • August 2022
  • July 2022
  • May 2022
  • March 2022
  • October 2021
  • April 2021
  • March 2021
  • January 2021
  • December 2020
  • October 2020
  • September 2020
  • August 2020
  • June 2020
  • May 2020
  • April 2020
  • March 2020
  • February 2020
  • January 2020
  • December 2019
  • November 2019
  • October 2019
  • September 2019
  • August 2019
  • May 2019
  • December 2018
  • November 2018
  • October 2018
  • September 2018
  • August 2018
  • July 2018
  • January 2018

Categories

  • Getting Started
  • Uncategorized

Copyright © 2026 Make Selenium Easy.

Powered by PressBook Masonry Dark