大佬们可以帮我看看这个程序哪里有问题吗?这是寻找字符串中出现最多的字母和次数的程序?

#include <stdio.h>
#include <stdlib.h>

/* run this program using the console pauser or add your own getch, system("pause") or input loop */

void find(char s[])
{
    int test[256];
    int i;
    int max=-1;
    char c;
    
    for(i=0;s[i];i++)
    {
        test[s[i]]++;
    }
    
    for(i=0;i<256;i++)
    {
        if(test[i]>max)
        {
            max=test[i];
            c=(char)i;
        }
    }
    printf("%c\n",c);
    printf("%d",max);
    
}

int main()
{
    char s[1000];
    
    scanf("%s",&s);
    find(s);         
        
    return 0;
}
回复
阅读 990
1 个回答

你代码唯一的问题是没对 test 做初始化,改成

int test[256] = { 0 };

就好了

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题
宣传栏