题解 | #计算某字符出现次数#
计算某字符出现次数
https://www.nowcoder.com/practice/a35ce98431874e3a820dbe4b2d0508b1
#include <iostream> using namespace std; int main() { string s; char asc; getline(cin,s); cin>>asc; int bsc = asc; int i = 0; int count = 0; if(bsc < 60){ while(s[i] != '\0'){ if(s[i] == asc){ count++; } i++; } } else if( bsc< 91){ int csc = bsc + 32; char Asc = csc; while(s[i] != '\0'){ if(s[i] == asc || s[i] == Asc){ count++; } i++; } } else{ int csc = bsc -32; char Asc = csc; while(s[i] != '\0'){ if(s[i] == asc || s[i] == Asc){ count++; } i++; } } cout<< count <<endl; } // 64 位输出请用 printf("%lld")
易错点:
- in不能判断输入空格,要用getline