统计出一个字符串每种字母出现的次数,并且打印出各字符对应的次数。注意:要求时间复杂度为O(n)。
举例如下:
输入字符串:“Hello!”
输出打印如下:
H:1
e:1
l:2
o:1
!:1
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include<time.h>
int main(void)
{
char s[100];
scanf("%s", s);
int asc[127] = { 0 };
for (int i = 0,length = strlen(s);i < length; i++)
{
asc[int (s[i])]++;
}
for (int j = 0;j < 127;j++)
{
if (asc[j]!=0)
{
printf("%c:%d\n",j,asc[j]);
}
}
system("pause");
return 0;
}
输出结果: