1159 字母出现频率

该篇文章介绍了如何使用C语言编写一个程序,统计用户输入文本中每个英文字母及其出现的频率,不区分大小写。
摘要由CSDN通过智能技术生成

题目描述

从键盘输入一行文本(小于1000字符),统计其中每个英文字母出现的频率,并输出出现过的英文字母及其次数,未出现过的不需要显示。为了简化问题的复杂度,假设在统计过程中不区分字母的大小写,即'A'与'a'被认为是一种字母。

输入要求

先从键盘输入一行文本。以换行符结束。

输出要求

输出统计结果。

输入样例

Studing C Language

输出样例

'A':2
'C':1
'D':1
'E':1
'G':3
'I':1
'L':1
'N':2
'S':1
'T':1
'U':2

提示

 

来源

NBU OJ
#include <stdio.h>
#include <string.h>

int main()
{
    char a[1001];
    int len,i,counta=0,countb=0,countc=0,countd=0,counte=0,countf=0,countg=0,counth=0,counti=0,countj=0,countk=0,countl=0,countm=0,countn=0,counto=0,countp=0,countq=0,countr=0,counts=0,countt=0,countu=0,countv=0,countw=0,countx=0,county=0,countz=0;
    gets(a);
    len=strlen(a);
    for(i=0;i<len;i++)
    {
        if(a[i]=='a'||a[i]=='A')
            {counta++;}
        if(a[i]=='b'||a[i]=='B')
            {countb++;}
        if(a[i]=='c'||a[i]=='C')
            {countc++;}
        if(a[i]=='d'||a[i]=='D')
            {countd++;}
        if(a[i]=='e'||a[i]=='E')
            {counte++;}
        if(a[i]=='f'||a[i]=='F')
            {countf++;}
        if(a[i]=='g'||a[i]=='G')
            {countg++;}
        if(a[i]=='h'||a[i]=='H')
            {counth++;}
        if(a[i]=='i'||a[i]=='I')
            {counti++;}
        if(a[i]=='j'||a[i]=='J')
            {countj++;}
        if(a[i]=='k'||a[i]=='K')
            {countk++;}
        if(a[i]=='l'||a[i]=='L')
            {countl++;}
        if(a[i]=='m'||a[i]=='M')
            {countm++;}
        if(a[i]=='n'||a[i]=='N')
            {countn++;}
        if(a[i]=='o'||a[i]=='O')
            {counto++;}
        if(a[i]=='p'||a[i]=='P')
            {countp++;}
        if(a[i]=='q'||a[i]=='Q')
            {countq++;}
        if(a[i]=='r'||a[i]=='R')
            {countr++;}
        if(a[i]=='s'||a[i]=='S')
            {counts++;}
        if(a[i]=='t'||a[i]=='T')
            {countt++;}
        if(a[i]=='u'||a[i]=='U')
            {countu++;}
        if(a[i]=='v'||a[i]=='V')
            {countv++;}
        if(a[i]=='w'||a[i]=='W')
            {countw++;}
        if(a[i]=='x'||a[i]=='X')
            {countx++;}
        if(a[i]=='y'||a[i]=='Y')
            {county++;}
        if(a[i]=='z'||a[i]=='Z')
            {countz++;}
    }
    if(counta!=0)
    {
        printf("\'A\':%d\n",counta);
    }
    if(countb!=0)
    {
        printf("\'B\':%d\n",countb);
    }
    if(countc!=0)
    {
        printf("\'C\':%d\n",countc);
    }
    if(countd!=0)
    {
        printf("'D':%d\n",countd);
    }
    if(counte!=0)
    {
        printf("'E':%d\n",counte);
    }
    if(countf!=0)
    {
        printf("'F':%d\n",countf);
    }
    if(countg!=0)
    {
        printf("'G':%d\n",countg);
    }
    if(counth!=0)
    {
        printf("'H':%d\n",counth);
    }
    if(counti!=0)
    {
        printf("'I':%d\n",counti);
    }
    if(countj!=0)
    {
        printf("'J':%d\n",countj);
    }
    if(countk!=0)
    {
        printf("'K':%d\n",countk);
    }
    if(countl!=0)
    {
        printf("'L':%d\n",countl);
    }
    if(countm!=0)
    {
        printf("'M':%d\n",countm);
    }
    if(countn!=0)
    {
        printf("'N':%d\n",countn);
    }
    if(counto!=0)
    {
        printf("'O':%d\n",counto);
    }
    if(countp!=0)
    {
        printf("'P':%d\n",countp);
    }
    if(countq!=0)
    {
        printf("'Q':%d\n",countq);
    }
    if(countr!=0)
    {
        printf("'R':%d\n",countr);
    }
    if(counts!=0)
    {
        printf("'S':%d\n",counts);
    }
    if(countt!=0)
    {
        printf("'T':%d\n",countt);
    }
    if(countu!=0)
    {
        printf("'U':%d\n",countu);
    }
    if(countv!=0)
    {
        printf("'V':%d\n",countv);
    }
    if(countw!=0)
    {
        printf("'W':%d\n",countw);
    }
    if(countx!=0)
    {
        printf("'X':%d\n",countx);
    }
    if(county!=0)
    {
        printf("'Y':%d\n",county);
    }
    if(countz!=0)
    {
        printf("'Z':%d\n",countz);
    }
    return 0;
}

有点太麻烦了,啧

  • 7
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以使用Fortran中的一些字符串处理函数和数组来实现读取文本字母出现频率的功能。 以下是一个基本的Fortran代码示例,它将读取一个包含文本的文件,并计算每个字母文本出现的次数: ``` program count_letters implicit none integer :: i, j, n character(len=1) :: letter character(len=100) :: filename, line integer, dimension(26) :: counts ! 初始化计数器 counts = 0 ! 提示用户输入文件名 write(*,*) '请输入文件名:' read(*,*) filename ! 打开文件并逐行读取 open(unit=10, file=filename, status='old') do read(10,'(A)',iostat=n) line if (n /= 0) then ! 遍历行中的每个字符并计数 do j=1,len_trim(line) letter = adjustl(adjustc(line(j:j))) if (len(letter) > 0) then counts(ichar(letter)-ichar('A')+1) = counts(ichar(letter)-ichar('A')+1) + 1 end if end do else exit end if end do close(10) ! 输出结果 write(*,*) '字母 出现次数' do i=1,26 write(*,*) char(i+ichar('A')-1), counts(i) end do end program count_letters ``` 在上面的代码中,我们首先定义了一个长度为26的整数数组 `counts`,用于存储每个字母文本出现的次数。然后,我们打开并逐行读取文件,并使用 `ichar` 函数将每个字符转换为其在ASCII表中的整数值。通过将整数值减去大写字母A的整数值,我们可以将每个字母映射到 `counts` 数组中的相应索引位置。最后,我们输出每个字母出现次数。 请注意,上面的代码仅考虑了大写字母。如果您想要计算文本中所有字母(包括小写字母和非字母字符)的出现频率,您需要相应地修改代码。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值