输出字符串各个小写字母对应个数

来源:互联网 发布:樱桃初夏网络剧blibli 编辑:程序博客网 时间:2024/05/11 14:26
#include <iostream>#include <assert.h>using namespace std;void CalcLowWord_Count(char *str){int max = 0;int count[26] = {0};int flag = 0;int flag2 = 0;assert(str);for(int i = 0; str[i] != '\0'; ++i){if(str[i] >= 'a' && str[i] <= 'z'){max = (++count[(int)str[i] - 97]) > max ? count[(int)str[i] - 97] : max;}}for(i = 1; i <= max; ++i){for(int j = 0; j < 27; ++j){if(count[j] == i){printf("%c",(char)(j + 97));flag = 1;}}if( flag ){printf(":%d\n",i);flag = 0;flag2 = 1;}}if(!flag2){printf("No matched!");}}int main(){char *str = "adOaadsfsgAsgBddgmnhkfdghfjfdhfshsfjhfgfpp";CalcLowWord_Count(str);return 0;}

0 0