搜索指针数组以计算字母使用

问题描述 投票:1回答: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]的数组。有什么建议吗?

c pointers
1个回答
0
投票

s[c]不是一个角色,它是一个pointer,你比较pointer和字符p这是无效的,

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

再旋转一个循环来比较每个字符串的字符串。

将您的代码修改为

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++;
        }
}

我希望你能得到这个。

© www.soinside.com 2019 - 2024. All rights reserved.