题解 | 计算某字符出现次数
#include <cctype> #include <cstdio> #include <iostream> #include <sstream> #include <string> using namespace std; int main() { string line; // 第一行输入 getline(cin, line); // 字符输入: char c; cin >> c; c = tolower(c); int count = -1; if (line.size() <= 1000 && line.size() >= 1) { count = 0; char tmpc; for (int i = 0; i != line.size(); ++i) { tmpc = line[i]; tmpc = tolower(tmpc); if (c == tmpc) { ++count; } } } cout << count << endl; return 0; }