满足以下条件的任何编程语言的答案
.
。例如:
输入:
mahatma gandhi
Mohndas KaramChand gandhi
输出:
M. Gandhi
M. K. Gandhi
答案 0 :(得分:1)
如果以下Java代码可以帮助您,请尝试。
class Main {
public static void main(String[] args) {
String input = "mahatma gandhi\n" +
"Mohndas KaramChand gandhi\nVijay singh";
char[] chars = input.toLowerCase().toCharArray();
boolean found = false;
for (int i = 0; i < chars.length; i++) {
if (Character.isWhitespace(chars[i])) {
chars[i + 1] = Character.toUpperCase(chars[i + 1]);
}
}
chars[0] = Character.toUpperCase(chars[0]);
String output = String.valueOf(chars);
String[] parts1 = output.split("\n");
for (int i = 0; i < parts1.length; i++) {
String[] names = parts1[i].split(" ");
String[] initials = output.split(" ");
String lastname = names[names.length - 1];
for (int j = 0; j < names.length - 1; j++) {
initials[j] = names[j].substring(0, 1);
System.out.print(initials[j] + ".");
}
System.out.println(" " + lastname);
}
}
}
输出
M. Gandhi
M.K. Gandhi
V. Singh
您可以尝试不同版本的输入字符串,或更改程序以从文件或控制台读取字符串。我希望这个对你有用。