2012-03-01 48 views
1

我正在开发一个小函数来显示(char)数组中最频繁的字符。 这就是我迄今为止所取得的成就,但我认为我走错了路。C - 查找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]; 
    } 


} 

} 

任何帮助?

+0

'test'不是“字符串”:它没有NUL终止符。将它用作字符串函数的参数是非法的。建议:用11个元素定义'test'或让编译器用'char test [] =“ciaociaoci”;'来计算元素。 – pmg 2012-03-01 09:43:26

+3

作业?如果是这样标记适当。 – Ricibob 2012-03-01 09:45:54

回答

5

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

} 
1

你传递一个(几乎)字符串和一个字符到strncmp()strncmp()需要两个字符串(和一个整数)。你的程序甚至不应该编译!

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

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

2

找到最常见的字,最简单的方法是创建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); 
+1

你可能想确保你不会尝试访问一个负面的索引(普通的'char'可能会被签名(在“127”后面加上负号))。你的数组可以统计'0'值,但不是'255'值。如果使用不同的符号相同的次数,则只有第一个这样的符号被报告为“最大字符”。 – pmg 2012-03-01 09:57:05

+0

该algorythm是错误的,请参阅下面的正确重写。 – Ldx 2012-04-24 12:03:03

0

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

0

我用结构做了一个工作版本。我猜,它工作正常,但我认为有更好的方法来编写这种算法。

#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); 


} 
1

假设一个输入数组为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; 
} 
0

我看到你所有的创造大数组和“复杂”的东西,所以我在这里很容易和简单的代码的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! 

}