从字符中统计出出现频率最多的汉字 (关键是知道汉字的存储方式)

#include <cstdlib>
#include <iostream>
#include<string>
using namespace std;
struct chnode
{
       char ch[3];
       int num;
}res[1000];
void getmostch(char s[],char ans[])
{
     char tmpch[3];
     int maxindex;
     int tmp = 0;
     int i,j;
     memset(res,0,sizeof(res));
     memset(tmpch,0,sizeof(tmpch));
     for(i = 0;i < strlen(s);i += 2)
     {
           memcpy(tmpch,&s[i],2);
           if(tmp == 0)
           {
                  strcpy(res[tmp].ch,tmpch);
                  res[tmp].num = 1;                  
                  tmp ++;
                  continue;
           }
           for(j = 0;j < tmp;j ++)
           {
                 if(strcmp(res[j].ch,tmpch) == 0)
                 {
                                            res[j].num ++;
                                            break;
                 }
           }
           if(j >= tmp)
           {
                strcpy(res[tmp].ch,tmpch);
                res[tmp].num = 1;                  
                tmp ++;
                continue;
           } 
     }
     maxindex = 0;
     for(i = 0;i < tmp;i ++)
     {
           printf("%d : %s %d\n",i,res[i].ch,res[i].num);
           if(res[i].num > res[maxindex].num)
           {
                         maxindex = i;
           }
     }
     strcpy(ans,res[maxindex].ch);
}
int main(int argc, char *argv[])
{
     char str[] = "天工网定位于我的建设门户、我的工作社区,倾听用户的声音,关注用户的需求是天工生存与发展的根基,网上会员大都是通过口碑相传得知天工网,并成为天工网的忠实用户";
     char mostch[3];
     getmostch(str,mostch);
     printf("the most appeared character is %s\n",mostch);
     system("PAUSE");
     return EXIT_SUCCESS;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值