题解 | #计算某字符出现次数#
计算某字符出现次数
https://www.nowcoder.com/practice/a35ce98431874e3a820dbe4b2d0508b1
#include <stdio.h>
#include <string.h>
int main() {
char str[1002] = {0};
char target;
char* cur = str;
int count[36] = {0}; //用于记录字符出现的次数.[a-z][0-9]
gets(str);
scanf("%c", &target);
while (*cur != '\0') { //遍历输入字符串,记录各个字符出现次数到count数组
if (*cur == ' ') {
cur++;
continue;
}
if (*cur >= 'A' && *cur <= 'Z') {
count[*cur - 'A']++;
} else if (*cur >= 'a' && *cur <= 'z') {
count[*cur - 'a']++;
} else if (*cur >= '0' && *cur <= '9') {
count[26 + *cur - '0']++;
}
cur++;
}
//根据目标值,输出对应的出现次数
if (target >= 'A' && target <= 'Z') {
printf("%d\r\n", count[target - 'A']);
} else if (target >= 'a' && target <= 'z') {
printf("%d\r\n", count[target - 'a']);
} else if (target >= '0' && target <= '9') {
printf("%d\r\n", count[26 + target - '0']);
}
return 0;
}

查看4道真题和解析