HJ2 题解 | #计算某字符出现次数#
计算某字符出现次数
https://www.nowcoder.com/practice/a35ce98431874e3a820dbe4b2d0508b1
#include<iostream>
#include<string>
using namespace std;
bool compare(int C, int W); //C表示输入的字符,W表示字符串;
int main(){
string word;
char s;
int i=0, j=0;
getline(cin,word);
cin>>s;
int len = word.length();
for(; i<len; i++){
if(compare(s, word[i]))
j++;
}
cout<<j;
return 0;
}
//问题关键在于假如C为0,ascall为48,W为字母P,ascall为80,那么P-32会等于0,导致识别错误,所以就是要区分数字和字母。
bool compare(int C, int W){
if(C>=48 && C<=57 && C==W) //48是0的ascll码,57是9的,即字符是数字,则必须C等于W
return true;
else if(C>=65 &&(C==W || C-32 == W || W-32==C)) //65是A的ascll码,大于等于65就是说明不是数字
return true;
else
return false;
}
华为机试刷题实录 文章被收录于专栏
记录一下本科应届生(我自己)刷华为机试题的过程
OPPO公司福利 1108人发布