2017-11-30 13 views
1

ですので、私は完全な答えを探していません。私はちょうどこれから始めるべきか分からない。私は名前の完全なポインタ配列を宣言しているコードを持っています。目的は、名前を検索し、使用されている各文字を数えるためのコードを書くことです。ポインタ配列を使用した文字数の検索は、

/*                    
* Search through each character in s,           
* which is array containing n strings,           
* and update the global count array            
* with the correct character counts.           
* Note: check the examples to see            
* if the counts should be case             
* sensitive or case insensitive.            
*/ 
void letterCount(char * s[], int n){ 
    //Implement this function              
    int c = 0,x;      // This is what I've done and I 
    char p = 'a', j = 'z';   // don't know where im messing up. 
    while (s[c] != '\0') {   // I know I can't compare pointer 
    if (s[c] >= p && s[c] <= j){ // and integer. 
     x = *s[c] - 'a'; 
      count[x]++; 
     } 
     c++; 
    } 
} 

/*                    
* Initialize each value in the global           
* count array to zero.               
*/ 
void initializeCount(){ 
    //Implement this function              
    int i; 
    for (i = 0; i < 26; i++){  // Also not sure if this is correct. 
    count[i] = 0; 
    } 
} 

出力では、count[26]という配列に文字を使用する必要があります。 お願いします。

+2

文字列は1文字以上の文字列です。長さは '\ 0'の最後の文字で示されます。そのため、少なくとも1つ( '\ 0'ターミネータのみがある場合)です。 (私にとって、あなたはこれを理解しているようです)あなたが考慮しなかったもの: 's'は文字列ではなく**文字列の配列**です。したがって、i = 0 ... 'n' - 1で' s [i] 'を反復する外側ループが存在しなければなりません。それぞれの' s [i] 'にコードを適用しなければなりません(char * "最初に説明したような文字列を指しています) – Scheff

+0

_「どこがうんざりしているのかわかりません」_問題の内容を指定するのを気にしなかったからです。エラーはありますか?それともうまくいくのですか? –

答えて

0

s[c]が文字ではない、それはpointerだ、あなたはpointerと有効でない文字pを比較している、

if (s[c] >= p && s[c] <= j) // here you are comparing address & char, which you shouldn't 

は、文字列の各文字を比較するための1つの以上のループを回転させます。

私はあなたがこれを持って期待し

void letterCount(char * s[], int n){ 
     //Implement this function              
     int c = 0,x,i;     // This is what I've done and I 
     char p = 'a', j = 'z';   // don't know where im messing up. 
     // assuming n is no of string, rotate main loop from 0 to n 
     while (c<n) {  
       for(i=0;s[c][i]!='\0';i++)   // I know I can't compare pointer 
         if (s[c][i] >= p && s[c][i] <= j){ // and integer. 
           x = s[c][i] - 'a'; 
           count[x]++; 
         } 
       c++; 
     } 
} 

ようにコードを変更します。

+0

's [c]!= '\ 0''は外側の条件ではまだ悪い考えですが、' n'は配列の文字列の数としての役割を果たすと思います。 – grek40

+0

@ grek40はい真実、私は正しく観察しなかった。文字列でないループを回転させることができます – achal

+0

"* s [c]は文字ではなく、文字列です*"、 's [c]'はポインタ、 'char'へのポインタ、' char * 'です。ポインタは配列ではありません。 – alk

関連する問題