从键盘输入n个字符串,统计每个字符串中出现次数最多的英文字母,输出该字母及其出现次数,以冒号分隔。说明:字母忽略大小写,出现次数最多的字母可能有多个,若字符串中没有英文字母,则输出“no letters”。
改好了,这下有b了
#include<stdio.h>
#include<string.h>
int main()
{
int n;
//printf("输入n\n");
scanf("%d",&n);
int i,j,k;
getchar();
for(i=0; i<n; i++)
{
int count[26]= {0};
char t[150];
gets(t);
int len=strlen(t);
for(j=0; j<len; j++)
{
if(t[j]>='A'&&t[j]<='Z')
{
count[t[j]-'A']++;
}
if(t[j]>='a'&&t[j]<='z')
{
count[t[j]-'a']++;
}
}
int max=count[0];
int maxp=0;
for(j=0; j<26; j++)
{
if(count[j]>max)
{
max=count[j];
maxp=j;
}
}
if(max==0)
{
printf("no letters\n");
}
else
{
printf("%c:%d\n",maxp+'a',count[maxp]);
for(k=0;k<26;k++)
{
if(count[k]==max&&k!=maxp)
{
printf("%c:%d\n",k+'a',count[k]);
}
}
}
}
return 0;
}
#include"stdio.h"
main()
{
int a[100]={0},i,j;
char c;
while((c=getchar())!='\n') /*获取字符并统计每个字母出现次数*/
for (i=65;i<=90;i++)
if(c==i||c==i+32) a[i]++ ;
for (j=65;j<=90;j++) /*输出统计信息*/
if (a[j]>0) printf("%c:%-3d\n",j,a[j]);
getch(); /*保持命令提示窗口不被自动关闭*/
}
代码如下,输入-1结束输入:
#include<stdio.h>
#include<string.h>
int main()
{
void number(char *p);
char str[100];
int n;
scanf("%d", &n);
getchar();
while(n--)
{
gets(str);
number(str);
}
return 0;
}
void number(char* p)
{
int num = 0, max;
char s[] = { 'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z' };
int a[26] = {0};
while (*p != '\0') //字符串的结束符号位"\0"
{
for (int i = 0; i < 26; i++)
{
if (*p == s[i] || *p == (s[i] - 32))
a[i]++;
}
p++;
}
max = a[0];
for(int i=0;i<26;i++)
if (a[i] > max)
{
num = i;
max = a[i];
}
for (int i = 0; i < 26; i++)
if(a[i]!=0)
printf("%c:%d\n", s[i], a[i]);
if (max == 0)
printf("no letters\n");
else
for (int j = 0; j < 26; j++)
if (a[j] == max)
printf("%c:%d\n", s[j], a[j]);
}
结果如图: