C - 查找char数组中最常见的元素

Ldx*_*Ldx 1 c arrays

我正在开发一个小函数来显示(char)数组中最常见的字符.这是我到目前为止所取得的成就,但我认为我走错了路.

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

int main()
{

char test[10] = "ciaociaoci";
max_caratt(test, 10);

}

int max_caratt(char input[], int size)
{
int i;
char max[300];
max[0] = input[0];

for (i=0; i<size; i++)
{

    if(strncmp(input,input[i],1) == 1)
    {
        printf("occourrence found");
        max[i] = input[i];
    }


}

}
Run Code Online (Sandbox Code Playgroud)

有帮助吗?

Ldx*_*Ldx 7

实际上,正确的代码是这样的.
它只是IntermediateHacker下面片段的更正版本.

void main()
{

int array[255] = {0}; // initialize all elements to 0

char str[] = "thequickbrownfoxjumpedoverthelazydog";

int i, max, index;

for(i = 0; str[i] != 0; i++)
{
   ++array[str[i]];
}


// Find the letter that was used the most
max = array[0];
index = 0;
for(i = 0; str[i] != 0; i++)
{
     if( array[str[i]] > max)
     {
         max = array[str[i]];
         index = i;
     }
}

printf("The max character is: %c \n", str[index]);

}
Run Code Online (Sandbox Code Playgroud)