统计字符串中字母出现的次数并打印最多的字母----C语言练习题

来源:互联网 发布:剑三能用优化补丁吗 编辑:程序博客网 时间:2024/05/01 15:48

题目要求:统计一个字符串中字母出现的次数,并且打印出现次数最多的,如果两个或多个字母同时出现次数一致且最多,同时打印。


程序示例:

#include "stdio.h"
#include "string.h"

int maxvalue(int *value,int index)  //返回数组中最大的元素
{

int i;
int j;
int max = 0;
for(i=0;i<index-1;i++){
for(j=i+1;j<index;j++){
if(value[i] <= value[j]){
if(max < value[j]){
max = value[j];
}
}
}
}
return max;
}

int main()
{
char buff[1024];
int count[26] = {0};
int i=0;
int max_count;

memset(buff,0,1024);
printf("Please input the string:");
scanf("%s",buff);
while(buff[i] != '\0'){
if(buff[i] >= 'a' && buff[i] <= 'z'){
count[buff[i] - 'a']++;
}
i++;
}

max_count = maxvalue(count,26);

for(i = 0;i < 26;i++){
if(count[i] == max_count){
printf("字母%c出现%d次!\n",'a'+i,count[i]);
}
}
}

程序已经通过编译,可正常运行。

原创粉丝点击