鱼C论坛

 找回密码
 立即注册
查看: 3025|回复: 1

[已解决]为什么程序运行之后只统计了小写的字母,没有统计到大写的字母?

[复制链接]
发表于 2021-11-7 22:27:29 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
题目:要求统计每个字母出现的次数,不区分大小写

代码:
  1. #include <stdio.h>

  2. struct Count
  3. {
  4.     char word[26];
  5.     int num[26];
  6. }count;


  7. void main()
  8. {
  9.     char string[]="AAABBcdzz",*str;
  10.     str=string;
  11.     for(int i=0;i<26;i++)
  12.     {
  13.         count.word[i]='a'+i;
  14.         count.num[i]=0;
  15.     }

  16.     for(int j=0;'a'+j<='z';j++)
  17.     {
  18.         for(int i=0;*(str+i)!='\0';i++)
  19.         {
  20.             if(*(str+i)==count.word[j] || *(str+i)==count.word[j]+32)
  21.             {
  22.                 count.num[j]++;
  23.             }
  24.         }
  25.     }

  26.     for(int n=0;n<26;n++)
  27.     {
  28.         if(count.num[n]!=0)
  29.         printf("%c  %d\n",count.word[n],count.num[n]);
  30.     }
  31. }
复制代码


运行结果:
  1. c  1
  2. d  1
  3. z  2
复制代码
最佳答案
2021-11-7 22:57:00
  1. #include <stdio.h>

  2. struct Count
  3. {
  4.     char word[26];
  5.     int num[26];
  6. }count;


  7. //void main()   // error
  8. int main(void)
  9. {
  10.     char string[]="AAABBcdzz",*str;
  11.     str=string;
  12.     for(int i=0;i<26;i++)
  13.     {
  14.         count.word[i]='a'+i;
  15.         count.num[i]=0;
  16.     }

  17.     for(int j=0;'a'+j<='z';j++)
  18.     {
  19.         for(int i=0;*(str+i)!='\0';i++)
  20.         {
  21.             //if(*(str+i)==count.word[j] || *(str+i)==count.word[j]+32)
  22.             //if(*(str+i)==count.word[j] || *(str+i)==count.word[j]-32)
  23.             if(str[i] == count.word[j] || str[i] == count.word[j] - 'a' + 'A')
  24.             {
  25.                 count.num[j]++;
  26.             }
  27.         }
  28.     }

  29.     for(int n=0;n<26;n++)
  30.     {
  31.         if(count.num[n]!=0)
  32.         printf("%c  %d\n",count.word[n],count.num[n]);
  33.     }

  34.     return 0;
  35. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2021-11-7 22:57:00 | 显示全部楼层    本楼为最佳答案   
  1. #include <stdio.h>

  2. struct Count
  3. {
  4.     char word[26];
  5.     int num[26];
  6. }count;


  7. //void main()   // error
  8. int main(void)
  9. {
  10.     char string[]="AAABBcdzz",*str;
  11.     str=string;
  12.     for(int i=0;i<26;i++)
  13.     {
  14.         count.word[i]='a'+i;
  15.         count.num[i]=0;
  16.     }

  17.     for(int j=0;'a'+j<='z';j++)
  18.     {
  19.         for(int i=0;*(str+i)!='\0';i++)
  20.         {
  21.             //if(*(str+i)==count.word[j] || *(str+i)==count.word[j]+32)
  22.             //if(*(str+i)==count.word[j] || *(str+i)==count.word[j]-32)
  23.             if(str[i] == count.word[j] || str[i] == count.word[j] - 'a' + 'A')
  24.             {
  25.                 count.num[j]++;
  26.             }
  27.         }
  28.     }

  29.     for(int n=0;n<26;n++)
  30.     {
  31.         if(count.num[n]!=0)
  32.         printf("%c  %d\n",count.word[n],count.num[n]);
  33.     }

  34.     return 0;
  35. }
复制代码

评分

参与人数 1荣誉 +4 收起 理由
redHmian + 4

查看全部评分

小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 1 反对 0

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-7-4 23:38

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表