0
点赞
收藏
分享

微信扫一扫

什么是Java的垃圾回收机制?

龙毓七七 2023-07-21 阅读 67

描述

写出一个程序,接受一个由字母、数字和空格组成的字符串,和一个字符,然后输出输入字符串中该字符的出现次数。(不区分大小写字母) 

数据范围: 1≤n≤1000 1≤n≤1000 

输入描述:

第一行输入一个由字母、数字和空格组成的字符串,第二行输入一个字符(保证该字符不为空格)。

输出描述:

输出输入字符串中含有该字符的个数。(不区分大小写字母)

示例1

输入:

ABCabc
A

复制输出:

2
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include <iostream>
#include<string>
#include<vector>
#include <map>
using namespace std;

int main() {
    string s;
    vector<string> v;
    map<char, int> mp;
    char  c;
    while (getline(cin,s)&&cin>> c) {
        for (int j = 0; j < s.size(); j++) {
            mp[s[j]] = 0;
        }
        for (int i = 0; i < s.size(); i++) {
            mp[s[i]]++;
        }
        int times = 0;
        map<char, int>::iterator it = mp.find(c);
        if (c >= 'a' && c <= 'z') {
            times = it->second;
            c = c - 32;
            it = mp.find(c);
            times += it->second;
            cout << times << endl;
        } else if (c >= 'A' && c <= 'Z') {
            times = it->second;
            c = c + 32;
            it = mp.find(c);
            times += it->second;
            cout << times << endl;
        } else if (c >= '0' && c <= '9') {
            times = it->second;
            cout << times << endl;
        }
    }
    return 0;
}
举报

相关推荐

0 条评论