字母统计(241)

描述现在给你一个由小写字母组成字符串,要你找出字符串中出现次数最多的字母,如果出现次数最多字母有多个那么输出最小的那个。

#include<stdio.h>
#include<string.h>
int main()
{
char str[1010];
int n, len, i, max, j;
scanf("%d",&n);
    while(n--)
    {
        int    a[26] = {0};
        max = 0;
        j = 0;
        scanf("%s",str);
        len = strlen(str);
        for(i = 0; i < len; i++)
        {
            a[str[i]-'a']++;
        }
        for(i = 0; i < 26; i++)
        if(max < a[i])
        {
             j = i;
            max = a[i];
        }
         printf("%c\n", j + 'a');
    }
 return 0;
}


这是题干:任务描述 本关任务:编写一个程序,从键盘上输入一篇英文文章,统计出其中的英文字母(不区分大小写)、数字和其他非空白字符的个数。 编程要求 根据提示,在右侧编辑器补充代码,用户首先输入英文文章的行数n(1≤n≤10),接着依次输入n行内容(每行少于80个字符)。要求统计出其中的英文字母(不区分大小写)、数字和其它非空格字符的个数。 测试说明 平台会对你编写的代码进行测试: 测试输入: 6 We focus on essay generation, which is a challenging task that generates a paragraph-level text with multiple topics. Progress towards understanding different topics and expressing diversity in this task requires more powerful generators and richer training and evaluation resources. 预期输出: 英文字母241 数字:0 其他字符:4 这是我写的代码:#include <iostream> #include <cstring> using namespace std; int main() { int n; int letternum=0; int numnum=0; int othernum=0; for(int i=0;i<n;i++){ char line[81]; cin.ignore(); cin.getline(line,81); int len=strlen(line); for(int j=0;j<len;j++){ char tmp=line[j]; if(tmp==' '||tmp=='\t') continue; if((tmp>='a'&&tmp<='z')||tmp>='A'&&tmp<='Z'){ letternum+=1; } else if(tmp>='0'&&tmp<=9){ numnum+=1; } else othernum+=1; } } cout<<"英文字母:"<<letternum<<endl; cout<<"数字:"<<numnum<<endl; cout<<"其他字符:"<<othernum<<endl; return 0; }但是英文字母那里总是数错为啥
最新发布
11-07
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值