题解 | #计算某字符出现次数#
计算某字符出现次数
https://www.nowcoder.com/practice/a35ce98431874e3a820dbe4b2d0508b1
#include <stdio.h>
#include <math.h>
#define MAXLEN 1024
#define ASVALUE 'a' - 'A'
_Bool compare(char a, char b){
if((a >='A' && a <= 'z') && (b >='A' && b <= 'z')){
if(abs(a - b) == ASVALUE)
return 1;
}
if(a - b == 0)return 1;
return 0;
}
int main() {
char string[MAXLEN];
char parmarChar = '\0';
int count = 0;
fgets(string,sizeof(string),stdin);
scanf("%c",&parmarChar);
for(int i = 0; i < strlen(string); i++){
if(compare(string[i],parmarChar))count++;
}
printf("%d",count);
return 0;
}
只有一个区分字母和其他字符区别的坑,因为字母不区分大小写,所以有两种可能,其它没啥
#日常刷题#
海康威视公司福利 1182人发布
查看19道真题和解析