可以整在一个循环里: #include <stdio.h> int main(void) { char str[1024] = {'\0'}; fgets(str, sizeof(str), stdin); char ch = getchar(); int str_len = strlen(str) - 1; if (str_len == 0) { printf("0\n"); return 0; } int count = 0, i = 0; for (i = 0; i < str_len; i++) { if (ch >= 'A' && ch <= 'Z') { if (str[i] == ch || str[i] == ch+32) count++; } else if (ch >= 'a' && ch <= 'z') { if (str[i] == ch || str[i] == ch-32) count++; } else { if (str[i] == ch) count++; } } printf("%d\n", count); return 0; }</stdio.h>
点赞
牛客网
牛客企业服务