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

 

问题描述:

#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 个答案:

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

int test[256] = { 0 };

就好了


CentOS 7.7编译安装,命令wget http://dl.amh.sh/amh.sh && bash amh.sh nginx-1.20,mysql-8.0,php-8.2报错 ...