题解 | #计算某字符出现次数#
计算某字符出现次数
https://www.nowcoder.com/practice/a35ce98431874e3a820dbe4b2d0508b1
#define _CRT_SECURE_NO_WARNINGS #include <stdio.h> void count_numbers_character( char* str, char ch) { //字符串由字母、数字、空格组成 char* curr = str; int count = 0; //count用于计算ch字符总数 if (ch >= 'A' && ch <= 'Z') { ch = ch + 32; //题目不区分大小写,大写字母转化为小写字母计数 } while (*curr) { if (*curr >= 'A' && *curr <= 'Z') { *curr = (*curr) + 32; //将字符串中大写字母转化为小写字母计数 } if (ch == *curr) { count++; } curr++; } printf("%d\n", count); } int main() { char str[1001]; gets(str); char ch = getchar(); count_numbers_character( str, ch); return 0; }