Title Case
Completed
Read a line of words and print it with each word capitalised (first letter upper case, rest lower case), words separated by single spaces.
Input
One line.
Output
One line.
Example
Input
hELLO wORLD from JAVA
Output
Hello World From Java
Editor
Java
Hints
Split into words, then rebuild them.
Solution explanation
Splitting, transforming and re-joining is a common text-processing pattern.
Show solution
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String[] words = sc.nextLine().trim().split("\\s+");
StringBuilder sb = new StringBuilder();
for (String w : words) {
if (sb.length() > 0) {
sb.append(' ');
}
sb.append(Character.toUpperCase(w.charAt(0))).append(w.substring(1).toLowerCase());
}
System.out.println(sb);
}
}