In this section, you will learn how to display the first letter of every word in uppercase.
In this section, you will learn how to display the first letter of every word in uppercase.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 capitalizeFirstLetter( String str ) { final StringTokenizer st = new StringTokenizer( str, " ", 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.append( token ); } return sb.toString(); } public static void main(String[]args) throws 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. |