题解 | #计算某字符出现次数#
计算某字符出现次数
https://www.nowcoder.com/practice/a35ce98431874e3a820dbe4b2d0508b1
#include <iostream> #include <string> using namespace std; int main() { string s; char c; int count=0; getline(cin,s); cin>>c; if((c>='a' && c<='z') || (c>='A' && c<='Z')){ char c2; if(c>='a' && c<='z'){ c2='A'+(c-'a'); }else{ c2='a'+(c-'A'); } for(int i=0;i<s.size();i++){ if(s[i]==c || s[i]==c2){ count++; } } }else{ for(int i=0;i<s.size();i++){ if(s[i]==c){ count++; } } } cout<<count<<endl; return 0; }