Share on Google+Share on Google+

Display first letter of every word in uppercase

Advertisement
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.

Advertisements

Advertisement

Posted on: October 26, 2009 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Share this Tutorial Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.