用java编程100道问题//14统计字母个数

统计字母个数

题目描述
给定一段文章,请输出每个小写字母出现的次数
输入
只有一组输入数据,该数据大小<10KB。该文章包括大小写字母、数字、标点符号等。文章以’#’结尾。
输出
输出格式为“C A”,C为’a’…’z’中的字母,A为出现次数,C和A之间空一格
样例输入 Copy
here is the input
this is the article#
样例输出 Copy
a 1
b 0
c 1
d 0
e 5
f 0
g 0
h 4
i 5
j 0
k 0
l 1
m 0
n 1
o 0
p 1
q 0
r 2
s 3
t 5
u 1
v 0
w 0
x 0
y 0
z 0

import java.util.Scanner;


public class Main {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner scan=new Scanner(System.in);
		int[] arr=new int[26];
		String s = "";
		while(!s.endsWith("#")){
			s=scan.nextLine();
			char ch=0;
			for(int i=0;i<s.length();i++){
				ch=s.charAt(i);
				if(ch<='z'&&ch>='a'){
					arr[ch-'a']++;
				}
				
			}
		}
		
		for(int i=0;i<26;i++){
			char num=(char) ('a'+i);
			System.out.println(num+" "+arr[i]);
		}
	}

}

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值