问题:
Input:-gandhi output:- Gandhi
Input:-mahatma gandhi output:- M. Gandhi
Input:-Mohndas Karamchand ganDhi output:- M. K. Gandhi
答案:
public class Chef_NITIKA {
static Scanner scan=new Scanner(System.in);
public static void main(String[] args) {
String name= myString();
System.out.println("nam is :"+name);
mySformatter(name);
}
private static void mySformatter(String name) {
int count=0;
for(char c:name.toCharArray()){
if(c==' '){
count+=1;
}
}
System.out.println(count+" blank spaces");
if(count==0){
char ch=name.charAt(0);
name=Character.toUpperCase(ch)+name.substring(1);
System.out.println("nam is :"+name);
}
else if(count==1){
char ch=name.charAt(0);
name= name.replace(' ', '.');
String subname=name.substring(name.indexOf(".")+1);
char c=subname.charAt(0);
subname=Character.toUpperCase(c)+subname.substring(1);
name=Character.toUpperCase(ch)+"."+subname;
System.out.println("nam is :"+name);
}
else if(count==2){
char ch=name.charAt(0);
// name= name.replace(' ', '.');
String subname=name.substring(name.indexOf(" ")+1);
System.out.println(subname);
String subsubname=subname.substring(name.indexOf(" "));
System.out.println(subsubname);
char c=subname.charAt(0);
char c1=subsubname.charAt(0);
subname=Character.toUpperCase(c)+subname.substring(1);
name = Character.toUpperCase(ch)+"."+Character.toUpperCase(c)+"."+Character.toUpperCase(c1)+subsubname.substring(1);
System.out.println("nam is :"+name);
}
}
private static String myString() {
System.out.println("enter the string");
String s=scan.nextLine();
StringBuffer name=new StringBuffer();
// name.append(s);
return s;
}
}
当我键入" abc cde fgh"时,我没有得到所需的输出。我得到输出为A.C..fgh
有没有有效的方法来解决这个问题?
不良输出: -
enter the string
iam writing onStack
nam is :iam writing onStack
2 blank spaces
writing onStack
ing onStack
nam is :I.W.Ing onStack
我希望输出为I.W.OnStack
答案 0 :(得分:2)
只需将名称拆分为组件,然后形成所需的缩写:
public static String getName(String input) {
String[] names = input.split("\\s+");
String output = "";
// replace 1st through second to last names with capitalized 1st letter
for (int i=names.length; i > 1; --i) {
output += names[names.length - i].substring(0, 1).toUpperCase() + ". ";
}
// append full last name, first letter capitalized, rest lower case
output += names[names.length - 1].substring(0, 1).toUpperCase()
+ names[names.length - 1].substring(1).toLowerCase();
return output;
}
public static void main(String[] args) {
System.out.println(getName("gandhi"));
System.out.println(getName("mahatma gandhi"));
System.out.println(getName("Mohndas Karamchand ganDhi"));
}
<强>输出:强>
Gandhi
M. Gandhi
M. K. Gandhi
在这里演示:
<强>更新强>
这是link to a demo,我已经更正了原始代码,至少部分是这样。问题是以下几行:
String subsubname = subname.substring(name.indexOf(" "));
我改为:
String subsubname = subname.substring(subname.indexOf(" ") + 1);
您没有正确识别名称第三部分的第一个字符。话虽如此,您目前的方法是冗长,难以阅读和不灵活。在实践中,您可能希望使用更精简的方法解决此问题。
答案 1 :(得分:2)
platforms/ios
答案 2 :(得分:0)
@Rushabh Oswal you can get desired result by below method
private static void mySformatter(String name) {
String [] tempArray=name.split(" ");
StringBuilder outputStr=new StringBuilder();
int len=tempArray.length;
for(int i=0;i<len;i++){
String str=tempArray[i];
if (str.isEmpty())
continue;
if(i==len-1)
outputStr.append(Character.toUpperCase(str.charAt(0))).append(str.substring(1).toLowerCase());
else
outputStr.append(Character.toUpperCase(str.charAt(0))).append(". ");
System.out.println(outputStr.toString());
}