题解 | #计算某字符出现次数#
计算某字符出现次数
https://www.nowcoder.com/practice/a35ce98431874e3a820dbe4b2d0508b1
#include <stdio.h>
//注意消解回车的影响
int main() {
//思路:对26个大小写字母进行hash映射,对0-9十个数字也进行映射
//对于空格不管,直接对于一行数据中的字母进行hash,找到对应的值然后输出
char c;
int hash[36]={};
while (scanf("%c",&c)&&c!='\n') {
if(c>='a'&&c<='z'){
hash[c-'a']++;
}
else if(c>='A'&&c<='Z'){
hash[c-'A']++;
}
else if(c>='0'&&c<='9'){
hash[26+c-'0']++;
}
}
scanf("%c",&c);
int res=0;
if(c>='a'&&c<='z'){
res = hash[c-'a'];
}
else if(c>='A'&&c<='Z'){
res = hash[c-'A'];
}
else if(c>='0'&&c<='9'){
res = hash[26+c-'0'];
}
printf("%d\n",res);
return 0;
}
