题解 | #输入n个整数,输出其中最小的k个#
计算某字符出现次数
http://www.nowcoder.com/practice/a35ce98431874e3a820dbe4b2d0508b1
#include <stdio.h>
#define CHARVALUE ('a' - 'A')
//判断字符大小写,小写返回0,大写返回1 int parsechar(char c) { if(c >= 'a' && c <= 'z' || c >= 'A' && c <= 'Z') { if(c <= 'Z') { return 1; } else { return 0; } } return -1; }
int main() { int i = 0; int count = 0; int flag = -1; char str[1000] = {0}; char c; gets(str); //输入字符串
scanf("%c", &c);
flag = parsechar(c);
while(str[i] != '\0')
{
if(flag == 1) //判断是大写字符
{
if(str[i] == c || str[i] == c+CHARVALUE)
{
count++;
}
}
else if(flag == 0) //判断是小写字符
{
if(str[i] == c || str[i] == c-CHARVALUE)
{
count++;
}
}
else //判断是数字或空格
{
if(str[i] == c)
{
count++;
}
}
i++;
}
printf("%d\n", count);
return 0;
}