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

作者站长头像
站长
· 阅读数 9
#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;
}
回复
1个回答
avatar
test
2024-07-11

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

int test[256] = { 0 };

就好了

回复
likes
适合作为回答的
  • 经过验证的有效解决办法
  • 自己的经验指引,对解决问题有帮助
  • 遵循 Markdown 语法排版,代码语义正确
不该作为回答的
  • 询问内容细节或回复楼层
  • 与题目无关的内容
  • “赞”“顶”“同问”“看手册”“解决了没”等毫无意义的内容