计算字符个数
计算字符个数
http://www.nowcoder.com/questionTerminal/a35ce98431874e3a820dbe4b2d0508b1
c
#include <stdio.h> int main(void) { char str[1000] = {0}, ch = 0; fgets(str, sizeof(str), stdin); ch = getchar();//fgetc(stdin); int str_len = strlen(str) - 1; if (str_len == 0) { printf("0\n"); return 0; } int count = 0, i = 0; if (ch >= 'A' && ch <= 'Z') { for (i = 0; i < str_len; i++) { if (str[i] == ch || str[i] == ch+32) count++; } } else if (ch >= 'a' && ch <= 'z') { for (i = 0; i < str_len; i++) { if (str[i] == ch || str[i] == ch-32) count++; } } else { for (i = 0; i < str_len; i++) { if (str[i] == ch) count++; } } printf("%d\n", count); return 0; }