杭电1219 i还可以这样玩

Sample Input
hello, this is my first acm contest!

Sample Output
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

#include<stdio.h>
void main(){
    
    
	char s[100005];//定义一个字符数组用来接收
	int i;
	while(gets(s)!=NULL){
    
    
		int b[1000]={
    
    0};//先全部都存0,用于计数
		for(i=0;s[i]!='\0';i++)
			b[s[i]]++;//在这里就把字符都变成了对应的ASC码,b[]数组是97开始的,到122
		for(i='a';i<='z';i++)
			printf("%c:%d\n",i,b[i]);
		printf("\n");
	}
}

tips
//gets接收字符串的时候会自动在最后加上\0,这也是在while里不用加清空s数组的步骤的原因
//eg: 第一次的储存状态contest\0 输入acm后的储存状态acm\0est\0
// \0不算一个长度,strlen测得的是不算\0的净长

猜你喜欢

转载自blog.csdn.net/cwindyc/article/details/107009664
今日推荐