题目1182:统计单词

题目描述:

编一个程序,读入用户输入的,以“.”结尾的一行文字,统计一共有多少个单词,并分别输出每个单词含有多少个字符。
(凡是以一个或多个空格隔开的部分就为一个单词)

输入:

输入包括1行字符串,以“.”结束,字符串中包含多个单词,单词之间以一个或多个空格隔开。

输出:

可能有多组测试数据,对于每组数据,
输出字符串中每个单词包含的字母的个数。

样例输入:
hello how are you.
样例输出:
5 3 3 3

 

 1 import java.util.Scanner;
 2  
 3 public class Main{
 4     public static void main(String[]args){
 5     Scanner in=new Scanner(System.in);
 6     while(in.hasNext()){
 7         String line=in.nextLine();
 8         char[]x=line.toCharArray();
 9         int cout=0;
10         for(int i=0;i<x.length;i++){
11         if(x[i]!=' '&&x[i]!='.'){
12             cout++;
13         }
14         else if(x[i]==' '&&x[i+1]==' '){
15         }
16         else if(x[i]=='.'){
17             System.out.println(cout);
18             break;
19         }
20         else if(x[i]==' '&&x[i+1]!=' '){
21             System.out.print(cout+" ");
22             cout=0;
23         }
24         }
25     }
26     }
27  }
28  
29 /**************************************************************
30     Problem: 1182
31     User: 0000H
32     Language: Java
33     Result: Accepted
34     Time:320 ms
35     Memory:29140 kb
36 ****************************************************************/

 

posted @ 2015-05-01 15:10  打小孩  阅读(229)  评论(0编辑  收藏  举报