在那个程序我会给出6个名字,然后我想从整个名字中找到频繁的字符。我试过这个,但是它并没有显示出所有名字中的字符,但整个名字都保留了下来。我想找到频繁的字母
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define max 6
int main()
{
char name[max][20];
char *ptrnaame;
int i,j,len, cnt, k, next, occurence=0, maximum=0;
char *index_max=NULL;
ptrname=name[0];
for(i=0; i<max; i++)
{
printf("Give the name :");
gets(name[i]);
len=strlen(name[i]);
while(len>20)
{
printf("Give the name again:");
gets(name[i]);
}
}
for(i=0; i<max; i++)
{
occurence=0;
for(j=i; j<20; j++)
{
if(!strcmp(*(name+i), *(name+j)))
{
occurence++;
}
}
if(occurence>maximum)
{
maximum=occurence;
index_max=*(name+i);
}
}
if(index_max!=NULL)
{
printf("The most frequent character is: %s with %d occurences", index_max, maximum);
}
system("pause");
return 0;
}
什么逻辑使用计数频率? – user2697817
如果你想知道哪个字母最常出现,你必须跟踪所有字母的出现。这意味着你需要一系列的计数,而不仅仅是一个标量。如果你只想计算字母的出现次数,你不必存储名字,只需处理它们,这些字母就是数字,因为它们来了。 –