Write a java program to convert the first letter of each word to capital case

Write a program to read a sentence in the string variable and convert the first letter of each word to the capital case. Print the final string.

Note: – Only the first letter in each word should be in the capital case in final string.

Input and Output Format:

Input consists of a string.

The output consists of a String (capitalized string).

Refer sample output for formatting specifications.

Sample Input:

hello every one

Sample Output:

Hello Every One

Java program to read a sentence in the string variable and convert the first letter of each word to the capital case. Print the final string

import java.util.Scanner;
import java.util.StringTokenizer;

public class MainClass {

 public static void main(String[] args) {
  // TODO Auto-generated method stub
  Scanner sc = new Scanner(System.in);
  String s1= sc.nextLine();      
        System.out.println(capsStart(s1));


 }
 public static String capsStart(String s1){
        StringBuffer sb=new StringBuffer();
        StringTokenizer t=new StringTokenizer(s1," ");
        while(t.hasMoreTokens()){
                        String s2=t.nextToken();
                        String s3=s2.substring(0,1);
                        String s4=s2.substring(1, s2.length());
                        sb.append(s3.toUpperCase()).append(s4).append(" ");                }
        return sb.toString();
 }
}

 

Add a Comment

Your email address will not be published. Required fields are marked *

seven + eighteen =