首页 > 试题广场 >

统计单词

[编程题]统计单词
  • 热度指数:17598 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 64M,其他语言128M
  • 算法知识视频讲解
编一个程序,读入用户输入的,以“.”结尾的一行文字,统计一共有多少个单词,并分别输出每个单词含有多少个字符。 (凡是以一个或多个空格隔开的部分就为一个单词)

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


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

输入

hello how are you.

输出

5 3 3 3
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String[] s = br.readLine().split(" ");
        int i;
        for (i = 0; i < s.length - 1; i++) {
            System.out.print(s[i].length() + " ");
        }
        System.out.println(s[i].length() - 1);
    }
}


发表于 2021-02-24 21:26:57 回复(0)
import java.util.Scanner;
public class Main{
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        while(sc.hasNext()){
            String str = sc.nextLine();
            String[] strs = str.split(" ");
            for(int i=0;i<strs.length;i++){
                if(i != strs.length-1){
                    System.out.print(strs[i].length());
                    System.out.print(" ");
                }else{
                    System.out.println(strs[i].length()-1);
                }
            }
        }
    }
}

发表于 2018-10-08 10:22:23 回复(0)
要想运行快,就不要用Scanner
发表于 2018-04-13 11:31:23 回复(0)

使用String的split方法,用正则表达式空格分开成小的字符串数组

import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        while(in.hasNext()) {
            String str = in.nextLine();
            String[] arr = str.split(" ");
            for(int i = 0; i < arr.length; i++) {
                if(i == arr.length - 1) {
                    System.out.print(arr[i].length() - 1);
                }
                else
                    System.out.print(arr[i].length() + " ");
            }
        }
    }

}
发表于 2018-04-05 21:41:28 回复(0)
import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        while (sc.hasNext()){
            String str = sc.nextLine();
            int temp = 0;
            
            for (int i = 0; i < str.length(); i++){
                if (str.charAt(i) != ' ' && str.charAt(i) != '.'){
                    temp++;
                }
                else if (str.charAt(i) == ' ' ){
                    System.out.print(temp + " ");
                    temp = 0;
                    continue;
                }
                else if (str.charAt(i) == '.'){
                    System.out.print(temp);
                    temp = 0;
                    break;
                }
            }
            System.out.println();
        }
    }
}

编辑于 2017-06-10 17:23:28 回复(1)
import java.util.Scanner;
public class Main {
	public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
		String str = scanner.nextLine();
		String[] result = str.split("\\s+");
		for (int i = 0,n = result.length; i < n; i++) {
			if (i == n - 1) {
				System.out.print(result[i].length() - 1);
			} else {
				System.out.print(result[i].length() + " ");
			}
		}
	}
}
//不明白为什么通过率1.00%,醉醉的

发表于 2017-03-02 17:37:29 回复(0)