繁体   English   中英

C-在char数组中查找最频繁的元素

[英]C - Find most frequent element in char array

我正在开发一个小功能来显示(字符)数组中最频繁的字符。 到目前为止,这是我已经完成的工作,但是我认为我走错了路。

#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];
    }


}

}

有什么帮助吗?

实际上,正确的代码是这样。
这只是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]);

}

查找最常见字符的最简单方法是创建一个255的int数组,然后递增与该字符对应的arraly元素。 例如:如果字符为'A',则增加'A'th元素(如果查看任何ascii表,您将看到字母'A'的十进制值为65)

int array[255] = {0}; // initialize all elements to 0
char str[] = "The quick brown fox jumped over the lazy dog.";
int i, max, index;
// Now count all the letters in the sentence
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[i] > max)
     {
         max = array[i];
         index = i;
     }
}

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

假设输入数组为0-127,则以下内容应使您在一次传递字符串时获得最常见的字符。 请注意,如果您要担心负数,请根据需要将所有内容上移+127 ...

char mostCommonChar(char *str) {

  /* we are making the assumption that the string passed in has values 
   * between 0 and 127.
   */
  int cnt[128], max = 0;
  char *idx = str;

  /* clear counts */
  memset((void *)cnt, 0, sizeof(int) * 128);

  /* collect info */
  while(*idx) {
    cnt[*idx]++;
    if(cnt[*idx] > cnt[max]) {
        max = *idx;
    }
    idx++;
  }

  /* we know the max */
  return max;
}

您正在将一个(几乎)字符串和一个char传递给strncmp() strncmp()需要两个字符串(和一个整数)。 您的程序甚至不应该编译!

建议:提高编译器的警告级别,并注意警告

您可能想看看strchr() ...

我使用结构制作了一个工作版本。 我猜它可以正常工作,但是我认为有一种更好的编写此算法的方法。

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

struct alphabet {
    char letter;
    int times;
};

typedef struct alphabet Alphabet;

void main() {

    char string[300];

    gets(string);


    Alphabet Alph[300];

    int i=0, j=0;

    while (i<=strlen(string)) {
        while(j<=300) {
            if(string[i] != Alph[j].letter) {
                Alph[i].letter = string[i];
                Alph[i].times = 1;
            }
            else {
                Alph[j].times++;
            }
            j++;
        }

        j=0;
        i++;
    }



    int y,max=0;
    char letter_max[0];
    for (y=0; y<strlen(string); y++) {

        printf("Letter: %c, Times: %d \n", Alph[y].letter, Alph[y].times);

        if(Alph[y].times>max) {
            max=Alph[y].times;
            letter_max[0]=Alph[y].letter;
        }

    }

    printf("\n\n\t\tMost frequent letter: %c - %d times \n\n", letter_max[0], max);


}

我看到你们所有人都在创建大型数组和“复杂”的东西,所以在这里我有简单的代码xD

char most_used_char (char s[]) {

    int i; //array's index
    int v; //auxiliary index for counting characters 

    char c_aux; //auxiliary character
    int sum = 0; //auxiliary character's occurrence 

    char c_max; //most used character
    int max = 0; //most used character's occurrence

    for (i = 0; s[i]; i++) { 

        c_aux = s[i];  

        for (v = 0; s[v]; v++) 
            if (c_aux == s[v]) sum++; /* responsible cycle for counting
                                         character occurrence */

        if (sum > max) { //checks if new character is the most used
            max = sum;
            c_max = c_aux;
        }

        sum = 0; /* reset counting variable so it can counts new  
                    characters occurrence */

    }

    return c_max; //this is the most used character!

}

如果不需要保留输入数组,可以先对输入数组进行排序,然后找到单个字符的最长连续行。 这种方法比较慢,但占用的空间较小。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM