/* String processing is the most common programming task in the actual development work. This topic is to require the program to the user input string processing. The specific rules are as follows: 1.
Capitalize the first letter of each word. 2. Separate the number from the letter with the underscore character (_) to make it clearer 3.
Adjust the number of spaces in the middle of the word to 1 spaces. For example: User input: You and me what Cpp2005program program output: You and me what Cpp_2005_program user input: This is a 99cat the program loses
Out: This is A 99_cat we assume that the user-entered string contains only lowercase letters, spaces, and numbers, excluding other letters or symbols.
Each word is separated by one or more spaces.
Suppose the user enters a string that is no more than 200 characters long.
* * Import Java.util.Scanner;
Import Java.util.regex.Matcher;
Import Java.util.regex.Pattern; public class Demo02 {public static void print (string[] s) {for (int i=0;i<s.length-1;i++) {System.out.print (s[i]+
" ");
} System.out.println (S[s.length-1]);
public static void Main (string[] args) {Scanner scan = new Scanner (system.in);
String s = scan.nextline ();
string[] ss = S.split ("[\\s]+"); for (int i=0;i<ss.length;i++) {String up = ("" +ss[i].charat (0)). toUpperCase ();
Capital StringBuffer sb = new StringBuffer (ss[i]);
Ss[i] = sb.replace (0, 1, up). ToString (); Matcher m = pattern.compile ("\\d+"). Matcher (Ss[i]);
while (M.find ()) {String num = new String (M.group ());
String num2 = num; Num2 = "_" +num+ "_";
Number before adding "_" ss[i] = ss[i].replace (num, num2);
if (Ss[i].startswith ("_")) {//GO Head "_" ss[i] = ss[i].substring (1);
} if (Ss[i].endswith ("_")) {//Go Tail "_" ss[i] = ss[i].substring (0,ss[i].length ()-1);
}} print (ss); }
}
Run Result:
You and me what cpp2005program and
me what Cpp_2005_program