题解 | 计算某字符出现次数
计算某字符出现次数
https://www.nowcoder.com/practice/a35ce98431874e3a820dbe4b2d0508b1
#include <stdio.h>
int isABC(char buf){
if((buf >= 'A') && (buf <= 'Z')) return 1;
if((buf >= 'a') && (buf <= 'z')) return 2;
if((buf >= '0') && (buf <= '9')) return 3;
return 0;
}
int main() {
char str[1024];
char buf = ' ';
char target = ' ';
int end=0;
int index = 0;
int counter = 0;
int type = 0;
while(isABC(buf)) scanf("%c", &buf);
str[0] = buf;
while(1)
{
scanf("%c",&buf);
if(isABC(buf)) {
str[end] = buf;
end++;
}
else if(buf==' '){
str[end] = buf;
end++;
}
else if(buf=='\n') break;
else continue;
}
scanf("%c",&target);
type = isABC(target);
for(index = 0;index <= end;index++)
{
switch (type){
case 1:if((str[index] == target)||(str[index] == target+32)){
counter++;
break;
}
else break;
case 2:if((str[index] == target)||(str[index] == target-32)){
counter++;
break;
}
else break;
case 3:if(str[index] == target) counter++;
else break;
default :break;
}
}
printf("%d",counter);
return 0;
}
查看6道真题和解析