0
点赞
收藏
分享

微信扫一扫

华为-计算字符个数


题目链接

​​https://www.nowcoder.com/practice/a35ce98431874e3a820dbe4b2d0508b1?tpId=37&tqId=21225&tPage=1&rp=&ru=/ta/huawei&qru=/ta/huawei/question-ranking​​

题目描述

写出一个程序,接受一个有字母和数字以及空格组成的字符串,和一个字符,然后输出输入字符串中含有该字符的个数。不区分大小写。

输入描述:

输入一个有字母和数字以及空格组成的字符串,和一个字符。

输出描述:

输出输入字符串中含有该字符的个数。

示例1

输入

复制

ABCDEF A

输出

复制

1

题解:

include <iostream>
#include <string>
using namespace std;
string tolow(string s){
for(int i = 0; i < s.length(); i++){
if(s[i] >= 'A'&&s[i] <= 'Z')
s[i] += 32;
}
return s;
}
int main(){
string s;
getline(cin, s);
s = tolow(s);
char x;
int count = 0;
cin >> x;
if(x >= 'A' && x <= 'Z')
x += 32;
for(int i = 0; i < s.length(); i++){
if(s[i] == x)
count++;
}
cout << count;
return 0;
}

 

举报

相关推荐

0 条评论