offer

1.计算字符个数

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

demo.cpp

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

int main()
{
    string str;
    char c;
    int N=0;
    getline(cin,str);
    cin >> c;
    if(tolower(c)>='a'&&tolower(c)<='z')
    {
       int N1=count(str.begin(),str.end(),c);
       int N2=count(str.begin(),str.end(),toupper(c));
       N = N1+N2;
    }
    else
    {
       int N=count(str.begin(),str.end(),tolower(c));
      
    }
    cout << N<<endl;
    return 0;
}

原文地址:https://www.cnblogs.com/voyagflyer/p/6435065.html