基于ASC码的字母统计程序,C++
#include <iostream>
#include <string>
using namespace std;
#define N 100
int main()
{
int num[26] = { 0 };
string arr[N] = { "0" };
int n;
cin >> n;
cin.ignore(); // 清空输入缓冲区
for (int i = 0; i < n; i++) {
getline(cin, arr[i]);
}
string fin = "";
for (int i = 0; i < n; i++) {
fin += arr[i];
}
int len = fin.length();
for (int i = 0; i < len; i++) {
for (int j = 0; j < 26; j++) {
if (fin[i] == static_cast<char>(65 + j) || fin[i] == static_cast<char>(97 + j)) {
num[j] += 1;
}
}
}
int flag = 0;
for (int i = 0; i < 26; i++)
{
if (num[i] != 0)flag = 1;
}
if (flag)
{
char n1[26] = { "0" };
int n2[26] = { 0 };
int t = 0;
for (int i = 0; i < 26; i++)
{
if (num[i] != 0)
{
n1[t] = static_cast<char>(97 + i); n2[t++] = num[i];
}
}
for (int i = 0; i < t - 1; i++)
{
cout << n1[i] << ":" << n2[i] << endl;
}
cout << n1[t - 1] << ":" << n2[t - 1];
}
else
{
cout<<"字符串中无字母。";
}
}