|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
题目:要求统计每个字母出现的次数,不区分大小写
代码:
- #include <stdio.h>
- struct Count
- {
- char word[26];
- int num[26];
- }count;
- void main()
- {
- char string[]="AAABBcdzz",*str;
- str=string;
- for(int i=0;i<26;i++)
- {
- count.word[i]='a'+i;
- count.num[i]=0;
- }
- for(int j=0;'a'+j<='z';j++)
- {
- for(int i=0;*(str+i)!='\0';i++)
- {
- if(*(str+i)==count.word[j] || *(str+i)==count.word[j]+32)
- {
- count.num[j]++;
- }
- }
- }
- for(int n=0;n<26;n++)
- {
- if(count.num[n]!=0)
- printf("%c %d\n",count.word[n],count.num[n]);
- }
- }
复制代码
运行结果:
- #include <stdio.h>
- struct Count
- {
- char word[26];
- int num[26];
- }count;
- //void main() // error
- int main(void)
- {
- char string[]="AAABBcdzz",*str;
- str=string;
- for(int i=0;i<26;i++)
- {
- count.word[i]='a'+i;
- count.num[i]=0;
- }
- for(int j=0;'a'+j<='z';j++)
- {
- for(int i=0;*(str+i)!='\0';i++)
- {
- //if(*(str+i)==count.word[j] || *(str+i)==count.word[j]+32)
- //if(*(str+i)==count.word[j] || *(str+i)==count.word[j]-32)
- if(str[i] == count.word[j] || str[i] == count.word[j] - 'a' + 'A')
- {
- count.num[j]++;
- }
- }
- }
- for(int n=0;n<26;n++)
- {
- if(count.num[n]!=0)
- printf("%c %d\n",count.word[n],count.num[n]);
- }
- return 0;
- }
复制代码
|
|