Statistical frequency of occurrence of characters

description

 

Write a statistical algorithm and the output frequency of occurrence of the various characters in the input string (character string valid number between 10 between the 26 letters AZ and 0-9).

Entry

A plurality of sets of data, each data a line, a character string is to be counted the frequency. When the string is "0", the input end.

Export

For each data output line n, the frequency of occurrence of each behavior of a character (characters exist only output format: Character: frequency), the order of characters in ascending order of ASCII code.

Sample input 1 

LITTLE5ILOVEYOU
AREYOUOKNUMB90077
0

Sample Output 1

5:1
E:2
I:2
L:3
O:2
T:2
U:1
V:1
Y:1
0:2
7:2
9:1
A:1
B:1
E:1
K:1
M:1
N:1
O:2
R:1
U:2
Y:1
#include <iostream>
#include <string>
using namespace std;

int main()
{
	while(1)
	{
	int n[36];int i;
	for(i=0;i<36;i++)
	n[i]=0;
	string s;
	cin>>s;
	if("0"==s) break;
	i=0;
	while(s[i]!='\0')
	{
		int e=(int) s[i];//key
		if(e>=65)
			n[e-65+10]++;
		if(e>=48&&e<=58)
			n[e-48]++;
		i++;
	}
	for(i=0;i<36;i++)
		if(n[i]) 
		{
			if(i<10)
			{
				cout<<i<<":"<<n[i]<<endl;
			}
			if(i>=10)
			{
				char e=(char)(i+65-10);
				cout<<e<<":"<<n[i]<<endl;
			}
			
		}
	}
	return 0;		
}

 

Published 100 original articles · won praise 4 · Views 3672

Guess you like

Origin blog.csdn.net/weixin_43673589/article/details/104409295