字符串:单词长度

来源:互联网 发布:stc单片机isp原理 编辑:程序博客网 时间:2024/06/06 03:13

你的程序要读入一行文本,其中以空格分隔为若干个单词,以‘.’结束。你要输出这行文本中每个单词的长度。这里的单词与语言无关,可以包括各种符号,比如“it's”算一个单词,长度为4。注意,行中可能出现连续的空格。


输入格式:

输入在一行中给出一行文本,以‘.’结束,结尾的句号不能计算在最后一个单词的长度内。


输出格式:

在一行中输出这行文本对应的单词的长度,每个长度之间以空格隔开,行末没有最后的空格。


输入样例:

It's great to see you here.


输出样例:

4 5 2 3 3 4



import java.util.Scanner;


public class Main {


public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner in =new Scanner(System.in);
boolean first=true;
String word=in.nextLine();//输入一句英语,每个单词之间有空格;
String [] a=word.split(" ") ;//空格把字符串分割开,放入一个字符串数组中
for(int i=0;i<a.length-1;i++)
{
if(a[i].length()!=0)
{
if(first)
System.out.print(a[i].length());
else
{
System.out.print(" "+a[i].length());
}
}
first=false;
}
if(first)//第一个输出的操作
{
System.out.print((a[a.length-1].length()-1));
}
else
System.out.print(" "+(a[a.length-1].length()-1));
}


}

原创粉丝点击