Java – Convert first character of every word to Uppercase

In this article, we will discuss how to convert first character of every word to uppercase

For conversion, we will use either

  1. StringTokenizer class
  2. split() method of String class

Note:

  • StringTokenizer is deprecated now but however it is carried forward for backward compatibility
  • Instead of StringTokenizer, developer should prefer using split() method of String class

Let us move forward to discuss to convert 1st character of every word to uppercase

1. Using StringTokenizer

Steps:

  1. Split the sample string into tokens using default space as delimiter
  2. Iterate through tokens using while loop
  3. Store each token into temp variable in each iteration
  4. And convert 1st character of each token into uppercase
  5. And append to StringBuffer by using append() method, along with single-space
  6. Finally pretty print to console

ConvertFirstCharacterOfEveryWordUsingStringTokenizer.java

package in.bench.resources.sb.operation;

import java.util.StringTokenizer;

public class ConvertFirstCharacterOfEveryWordUsingStringTokenizer{

	public static void main(String[] args) {

		// create StringBuffer object to store converted strings
		StringBuffer sbuffer = new StringBuffer();

		// sample string
		String str = "who will be answerable"
				+ " at the end of the day";

		// create StringTokenizer with above content
		StringTokenizer st = new StringTokenizer(str);

		while(st.hasMoreElements()) {

			// store it in temporary variable
			String temp = st.nextToken();

			// convert 1st character into upper-case
			String firstUppercase = Character.toUpperCase(
					temp.charAt(0)) + temp.substring(1);

			// add converted string first
			sbuffer.append(firstUppercase);

			// and then add single space
			sbuffer.append(" ");
		}

		// finally pretty print to console
		System.out.println(sbuffer.toString().trim());
	}
}

Output:

Who Will Be Answerable At The End Of The Day

2. Using split() method of String class

Steps:

  1. Split the sample string by specifying space as delimiter and store it into String[] array
  2. Iterate through string array using for-loop
  3. Store each string into temp variable in each iteration
  4. And convert 1st character of each string into uppercase
  5. And append to StringBuilder by using append() method, along with single-space
  6. Finally pretty print to console

ConvertFirstCharacterOfEveryWordUsingSplitMethod.java

package in.bench.resources.sb.operation;

public class ConvertFirstCharacterOfEveryWordUsingSplitMethod {

	public static void main(String[] args) {

		// create StringBuilder object to store converted strings
		StringBuilder sbuilder = new StringBuilder();

		// sample string
		String str = "this world has very good leader"
				+ " only that they need to be identified";

		// create StringTokenizer with above content
		String[] strArray = str.split("\\s");

		for(int index=0; index < strArray.length; index++) {

			// store it in temporary variable
			String temp = strArray[index];

			// convert 1st character into upper-case
			String firstUppercase = Character.toUpperCase(
					temp.charAt(0)) + temp.substring(1);

			// add converted string first
			sbuilder.append(firstUppercase);

			// and then add single space
			sbuilder.append(" ");
		}

		// finally pretty print to console
		System.out.println(sbuilder.toString().trim());
	}
}

Output:

This World Has Very Good Leader
Only That They Need To Be Identified

Hope, you found this article very helpful. If you have any suggestion or want to contribute any other way or tricky situation you faced during Interview hours, then share with us. We will include that code here.

Related Articles:

References:

Happy Coding !!
Happy Learning !!

Java - StringTokenizer class with example
Java - Difference between capacity() and length() methods of StringBuffer ?