#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;
}