天之道

享受编程的乐趣。
posts - 118, comments - 7, trackbacks - 0, articles - 0
  C++博客 :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理

字符频率

Posted on 2012-08-18 11:43 hoshelly 阅读(195) 评论(0)  编辑 收藏 引用 所属分类: Programming
编写一程序,接受一个字符串作为参数,并打印一张表。对于在字符串中出现的每个字符,该表给出该字符以及它的出现频率。

代码测试通过:

#include<stdio.h>
#define N 1000
int main()
{
    char a[N];
    int b[N]={0};
    int i,c[N]={0};
    printf("Input the string: ");
    gets(a);
    for(i=0;a[i]!='\0';i++)
    {
        b[a[i]]++;
    }
    for(i=0;a[i]!=0;i++)
    {
        if(c[a[i]] == 0)
        {
           printf("%c : %d\n",a[i],b[a[i]]);
           c[a[i]]=1;
        }
    }
    printf("\n");
    return 0;
}

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理