3 回答
TA贡献7条经验 获得超1个赞
#include<iostream> using namespace std; void main() { char s[100], Letter; cout << "输入字符串:"; cin >> s; int count[26] = { 0 }, i, s_i; // 统计26个字母出现的次数 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; // 输出 } } }
TA贡献4条经验 获得超1个赞
#include<iostream>
#include<string>
using namespace std;
int main()
{
cout << "请输入字符串:" << endl;
string s;
getline(cin, s);
int count = s.size();
char letter[26] = { 'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z' };
for (int i = 0; i < 26; i++)
{
int num = 0;
for (int j = 0; j < count; j++)
{
if (s[j] == letter[i])
{
num++;
}
}
if (num > 0)
{
cout << letter[i] << ":" << num << endl;
}
num = 0;
}
system("pause");
return 0;
}
- 3 回答
- 0 关注
- 1309 浏览
添加回答
举报