牛客网 - 在线编程 - 华为机试 - 计算字符个数

题目描述:

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

输入描述:

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

输出描述:

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

示例1

输入

ABCDEF A

输出

1

C++:

#include <iostream>
#include <string>
using namespace std;

int main()
{
	string s;
	char c;
	cin >> s;
	cin >> c;
	int count = 0;
    int i = 0;
    while (s[i] != '\0')
    {
        if (c == s[i])
            count++;
        else if (s[i] >= 'A' && s[i] <= 'Z' && c == s[i] - 'A' + 'a')
            count++;
        else if(s[i] >= 'a' && s[i] <= 'z' && c == s[i] - 'a' + 'A' )
            count++;
        i++;
    }
	cout << count << endl;
	return 0;
}

注意边界条件

Python:

print(input().split(' ')[0].lower().count(input().split(' ')[-1].lower()))

a.count(b): 计算a中b的个数

a.lower(): 全部化成小写

猜你喜欢

转载自blog.csdn.net/qq_39735236/article/details/81564848