/*
*input:tester
*output:e
*/
#include<stdio.h>
#include<stdlib.h>
//统计字符串中出现次数最多的字母,如果字母出现次数相同,则按a-z字典序输出第一个。
int countCharacter(const char * str)
{
int i=0,max=str[0]-'a';
int num[26]={0};
while(str[i] != '\0')
{
int x = str[i]-'a';
num[x]++;
if(num[x] > num[max])
{
max=x;
i++;
}else if(num[x] == num[max] && x < max){
max=x;
i++;
}else{
i++;
}
}
return max;
}
int main()
{
int re;
const char *str = (char *)malloc(sizeof(char)*100);
printf("input:");
scanf("%s",str);
re = countCharacter(str);
printf("output:%c\n",'a'+re);
return 0;
}
统计字符串中出现次数最多的字母并输出
最新推荐文章于 2023-12-29 17:26:49 发布