2012-03-01 11 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

+3

宿題?もしそうなら、適切にタグを付けます。 – Ricibob

答えて

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

(ほとんど)文字列とcharをstrncmp()に渡しています。 strncmp()は2つの文字列(および整数)を取ります。あなたのプログラムはコンパイルすべきではありません!

提案:コンパイラの警告レベルを上げ、は警告を気にしてください。

あなたは strchr()を見てみたいことがあり

...

2

最も一般的な文字を見つけるための最も簡単な方法は、255のint配列を作成し、ただ文字に対応するarraly要素をインクリメントすることです。たとえば:charcterが「A」であれば、その後、インクリメント「A番目の要素を(あなたが任意のASCIIテーブルを見ればあなたは文字がいることがわかります 『』 65の10進値を持つ)

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の後ろには負の ")。 'array'は' 0'の値を数えますが、 '255'の値は数えません。異なるシンボルが同じ回数だけ使用された場合、最初のそのようなシンボルだけが「最大文字」として報告されます。 – pmg

+0

algorythmが間違っています。下記の正しい書き換えを参照してください。 – Ldx

0

入力配列を保持する必要がない場合は、入力配列を最初にソートしてから、最長の連続した1文字の文字列を見つけることができます。このアプローチはより遅くなりますが、より少ないスペースを使用します。

0

私はstructを使って作業バージョンを作った。それは正常に動作しますが、私はこのアルゴリズムを書くにはもっと良い方法があると思います。

#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であると仮定すると、文字列を1回通して最も一般的な文字が得られるはずです。あなたは、負の数を心配し、必要に応じて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! 

} 
関連する問題