Below is the syntax highlighted version of Capitalize.java
from §3.1 Using Data Types.
/****************************************************************************** * Compilation: javac Capitalize.java * Execution: java Capitalize < input.txt * * Read in a sequence of words from standard input and capitalize each * one (make first letter uppercase; make rest lowercase). * * % java Capitalize * now is the time for all good * Now Is The Time For All Good * to be or not to be that is the question * To Be Or Not To Be That Is The Question * * Remark: replace sequence of whitespace with a single space. * ******************************************************************************/ public class Capitalize { public static String capitalize(String s) { if (s.length() == 0) return s; return s.substring(0, 1).toUpperCase() + s.substring(1).toLowerCase(); } public static void main(String[] args) { while (!StdIn.isEmpty()) { String line = StdIn.readLine(); String[] words = line.split("\\s"); for (String s : words) { StdOut.print(capitalize(s) + " "); } StdOut.println(); } } }