题解 | #计算某字符出现次数#
计算某字符出现次数
https://www.nowcoder.com/practice/a35ce98431874e3a820dbe4b2d0508b1
分清大小写和数字
#include <cstdio> #include <iostream> #include <string> using namespace std; int main() { string s; getline(cin, s); char check = getchar(); int len = size(s); int count = 0; // 小写:2,大写:1 数字 0 int flag = (check>='a'&&check<='z')?2:(check>='A'&&check<='Z')?1:0; if(flag == 2){ char upperCase = check-32; for(int i = 0; i < len; i ++){ if(s[i]==check || s[i]==upperCase) count++; } }else if(flag == 1){ char lowerCase = check + 32; for(int i = 0; i < len; i ++){ if(s[i] == check || s[i] == lowerCase) count++; } }else{ for(int i = 0; i < len; i ++) if(s[i] == check) count++; } cout << count << endl; } // 64 位输出请用 printf("%lld")