Java Data Type How to - Capitalize the first letter of each word in a string








Question

We would like to know how to capitalize the first letter of each word in a string.

Answer

import java.util.Scanner;
/*from  w  w w. j  a v a2 s  . c  o m*/
public class Main {
  public static void main(String[] args) {
    System.out.println("Enter a string");
    Scanner input = new Scanner(System.in);
    String s1 = input.nextLine();
    s1 = s1.trim();
    int howLong = s1.length();

    for (int counter = 0; counter < howLong; counter++) {
      char ch = s1.charAt(counter);
      System.out.print(ch);
    }

  }
}