Home Tutorial Java Core Display first letter of every word in uppercase

 
 

Display first letter of every word in uppercase
Posted on: October 26, 2009 at 12:00 AM
In this section, you will learn how to display the first letter of every word in uppercase.

Display first letter of every word in upper case

In this Java tutorial section, you will learn how to display the first letter of every word in uppercase. For this, we have allowed the user to enter the string. The BufferedReader class read the string from the console. This string is then converted into tokens using the class StringTokenizer. The method token.charAt(0) return the first character of every token and the  method toUpperCase(token.charAt(0)),token.substring(1) ) of Character class change the first character of every token to uppercase.

Here is the code:

import java.util.*;
import java.io.*;

public class FirstLetter{
public static String capitalizeFirstLetterString str ) {
final StringTokenizer st = new StringTokenizerstr, " "true );
final StringBuilder sb = new StringBuilder();

while st.hasMoreTokens() ) {
String token = st.nextToken();
Character ch=Character.valueOf(token.charAt(0));
token = String.format"%s%s",Character.toUpperCase(token.charAt(0)),token.substring(1) );
sb.appendtoken );
}
return sb.toString();
}
public static void main(String[]argsthrows Exception{
System.out.print("Enter the sentence : ");
BufferedReader br =new BufferedReader(new InputStreamReader(System.in));
String inputString = br.readLine();
FirstLetter letter=new FirstLetter();
String words=letter.capitalizeFirstLetter(inputString);
System.out.println(words);
}
}

Output:

Enter the sentence: java is programming language.
Java Is A Programming Language.

Related Tags for Display first letter of every word in uppercase:


Ask Questions?

If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.

Ask your questions, our development team will try to give answers to your questions.