第1个回答 2017-12-29
#include<iostream>
using namespace std;
void main()
{
char s[100], Letter;
cout<<"输入字符串:";
cin>>s;
int count[26], i, s_i; // 统计26个字母出现的次数
// 初始化
for (i=0; i<26; i++)
count[i]=0;
// 统计各字母出现次数
int n=strlen(s);
for (i=0; i<n; i++)
{
s_i=s[i]-'a';
count[s_i]++; // 字符
}
// 输出结果
for (i=0; i<26; i++)
{
if (count[i] != 0 ) // 若该字母出现。。。
{
Letter='a'+i;
cout<<Letter<<" : "<<count[i]<<endl; // 输出字母及出现次数
}
}
}
运行示例:
输入字符串:aaddffadf
a : 3
d : 3
f : 3
如果统计的字符不止是26个小写字母,只需相应改变 cout[] 的大小就行了。本回答被网友采纳