在控制台输入He took a glance over the notes.(字符串我随便找的,格式是这样)
要求是输出字符串中每个单词的长度,
单词与单词之间的空格可能存在多个,
.不能算在最后一个单词当中,
输出的结果是:2 4 1 6 4 3 5
最后不能有空格
class Test {
public static void main(String[] args) {
String line = "He took a glance over the notes.";
line = line.trim();
line = line.replace("\\.", "");
String[] ss = line.split("[ ]+");
for (int i = 0; i < ss.length; ++i) {
System.out.print(ss[i].length() + " ");
}
}
}
|
|