Ignatius现在正在做作业。老师给了他一些文章,并要求他说出每个字母的出现次数。 真的很简单,不是吗?
输入格式:
多组输入。每一篇文章只有一行,所有的字母都是小写字母。你只需要计算每个字母的数量,所以不要注意其他字符。文章的长度最多是100000。处理到文件的末尾。
输出格式:
对于每一篇文章,你必须说出每封信的出现次数。输出格式类似“x:n”。
输入样例:
hello, this is my first acm contest!
work hard for hdu acm.
输出样例:
a:1
b:0
c:2
d:0
e:2
f:1
g:0
h:2
i:3
j:0
k:0
l:2
m:2
n:1
o:2
p:0
q:0
r:1
s:4
t:4
u:0
v:0
w:0
x:0
y:1
z:0
a:2
b:0
c:1
d:2
e:0
f:1
g:0
h:2
i:0
j:0
k:1
l:0
m:1
n:0
o:2
p:0
q:0
r:3
s:0
t:0
u:1
v:0
w:1
x:0
y:0
z:0
代码 如下
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
// TODO Auto-generated method stub
//字符a的ASCII是97
Scanner sc=new Scanner(System.in);
while(sc.hasNextLine()) {
int num[]=new int[26];
String s=sc.nextLine();
if(s.equals(""))
break;
else {
for(int i=0;i<s.length();i++) {
if(s.charAt(i)>='a'&&s.charAt(i)<='z')
num[(int)s.charAt(i)-97]++;
}
for(int j=0;j<26;j++)
System.out.println((char)(j+97)+":"+num[j]);
}
}
}
}