/*具体规则如下:
1. 把每个单词的首字母变为大写。
2. 把数字与字母之间用下划线字符(_)分开,使得更清晰
3. 把单词中间有多个空格的调整为1个空格。
例如:
用户输入:
mutua4lly orthog6onal basis fu12nctions
则程序输出:
Mutua_4_lly Orthog_6_onal Basis Fu_12_nctions
用户输入:
我们假设:用户输入的串中只有小写字母,空格和数字,不含其它的字母或符号。
每个单词间由1个或多个空格分隔。
1. 把每个单词的首字母变为大写。
2. 把数字与字母之间用下划线字符(_)分开,使得更清晰
3. 把单词中间有多个空格的调整为1个空格。
例如:
用户输入:
mutua4lly orthog6onal basis fu12nctions
则程序输出:
Mutua_4_lly Orthog_6_onal Basis Fu_12_nctions
用户输入:
euclidean 345distances can be best 112preserved
则程序输出:
euclidean 345distances can be best _112_preserved我们假设:用户输入的串中只有小写字母,空格和数字,不含其它的字母或符号。
每个单词间由1个或多个空格分隔。
假设用户输入的串长度不超过200个字符。*/
//主要使用的是 正则表达式的知识
package Al2014;
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class AL2 {
/**
* @param args
*/
public static void main(String[] args) {
String str="";
str=(new Scanner(System.in)).nextLine();
StringBuffer sb=new StringBuffer();
String[] str1=str.split("[ ]+");//只匹配方括号中的空格,+表示一个或多个空格
String s="";
for(int i=0;i<=str1.length-1;i++)
{
if((str1[i].charAt(0)>='a')&&(str1[i].charAt(0)<='z')){
s+=String.valueOf((char)(str1[i].charAt(0)+('A'-'a')))+str1[i].substring(1)+" ";
}
else if((Integer.parseInt(String.valueOf(str1[i].charAt(0))) >=0)&&(Integer.parseInt(String.valueOf(str1[i].charAt(0))) <=9))
{
s+=str1[i]+" ";
}
}
//System.out.println(s);输出符合规范的
Pattern p=Pattern.compile("([0-9]+)");//start()返回匹配到的子字符串在字符串中的索引位置.
// end()返回匹配到的子字符串的最后一个字符在字符串中的索引位置.
// group()返回匹配到的子字符串
Matcher m=p.matcher(s);
int st1=0;
String fin="";
while(m.find())
{
int st=m.start();
int end=m.end();
fin+=s.substring(st1,st);
if(s.charAt(st-1)!=' ') fin+="_";
fin+=m.group(1);
if(s.charAt(end)!=' ') fin+="_";
st1=end;
}
if(st1<s.length())//比较索引
fin+=s.substring(st1);
System.out.println(fin);
}
}