65,187
社区成员




#include <iostream>
#include <string>
#include <vector>
using namespace std;
void frequency(string s)
{
vector<char> a; //记录字符串中有多少不同的字符
vector<int> num; //记录每种字符出现的个数
int j = 0;
int k = 0; //返回不同字符数
if(!a.empty() || !num.empty())
cout << "Initialize is failed!" << endl;
else
{
if(s.length()==0)
{
cout << "My god! This is an empty string!" << endl;
}
else
{
//统计字符
for(int i=0; i<s.length(); i++)
{
while(j<k && a[j]!=s[i])
j++;
if(j == k)
{
a[k] = s[i];
num[k]++;
k++;
}
else
num[j]++;
}
//输出结果
for(int i=0; i<k; i++)
{
cout << a[i] << '\t' << num[i] << endl;
}
}
}
}
void main()
{
cout << "Input a string for the calculation:";
string str;
getline(cin, str);
frequency(str);
system("pause");
}