您的位置:首页 > 其它

每个单词间一个空格,首字母大写

2016-11-07 13:53 369 查看
//First,change the words to the standard form,where every word is separated by only one blank
//Second,change every word's first alphabet to the upper
import java.util.*;
import java.lang.*;
class Deal{
String deleteBlank(String words){
int flag=0;
words=words.trim();
StringTokenizer temp = new StringTokenizer(words," ");
String result=new String("");
while(temp.hasMoreTokens()){
if(flag==1)
result+=" ";
result+=temp.nextToken();
flag=1;
}
return result;
}
String changeFirstAlphabet(String words){
int in=0;
char array[]=words.toCharArray();
for(int i=0;i<array.length;i++){
if(array[i]!=' '){
if(in==0&&Character.isLowerCase(array[i]))
array[i]=Character.toUpperCase(array[i]);
in=1;
}
else
in=0;
}
String result=new String(array);
return result;
}

}
class FirstAlphabetUpper{
public static void main(String []args){
String words;;
Scanner reader = new Scanner(System.in);
Deal deal = new Deal();
words=reader.nextLine();
words=deal.deleteBlank(words);
words=deal.changeFirstAlphabet(words);
System.out.println(words);
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐