Write a Java program, the sentence entered by the user among the first letter of each word capitalized, while the rest of the word in lowercase letters.
Requirements: Running the program requires the user to enter a sentence. Then extract each word, the first letter of a word to uppercase and the rest lowercase characters. Use StringBuffer class to create a new string to replace over-sensitive. New final output string.
Hint: Use the Scanner nextLine () method to obtain a sentence. Note: the word delimiter spaces addition, there may or Tab (,; punctuation.). Note judge first character is not a letter. Other letters in a word may be capitalized.
For example: when the user inputs "This is a samPLe sentencE to demostrATE the TasK 2."
program should Output: This Is A Sample Sentence To Demonstrate The Task 2.
code show as below
package CaseSubstitution;
import java.util.Scanner;
/**
*
* @author 芳芳
*
*/
public class CaseSubstitution {
public static void main(String[] args) {
@SuppressWarnings("resource")
Scanner input = new Scanner(System.in);
System.out.println("please input a string:");
String s = input.nextLine();
System.out.println("converted result is as follows:");
si(s);
}
private static void si(String s) {
String[] split = s.split(" ");
//按空格分隔成数组
for (int i = 0; i <split.length ; i++) {
// .substring(0,1) 截取数组的首字母 +split[i].substring(1); 再加上后面
String s2 = split[i].substring(0, 1).toUpperCase()+split[i].substring(1);
System.out.print(s2+" ");
}
}
}