简体   繁体   English

还将大写转换为小写进行计数

[英]Also convert upper case to lower case to count

I have a code that I am supposed to make it go through a list of names and count how many times a letter is used. 我有一个代码,可以使它遍历名称列表并计算使用字母的次数。 So far it works for lower case but how do I implement upper case into the picture. 到目前为止,它适用于小写字母,但如何在图片中实现大写字母。 I greatly appreciate the help. 我非常感谢您的帮助。

/*                                                                              
 * 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,i;
  char p = 'a', j = 'z', A = 'A', Z = 'Z';
  while (c<n) {
    for(i=0;s[c][i]!='\0';i++){
      if (s[c][i] >= p && s[c][i] <= j ){
        x = s[c][i] - 'a';
        count[x]++;
      }
      c++;
    }
  }
}

You can set a criteria for casing of letters, ie, only use lower case or upper case letters. 您可以设置字母大小写的条件,即仅使用小写或大写字母。 Convert the contents of character array to lower case(by aading 32 to each character's value(ASCII VALUE)) then call letterCount function. 将字符数组的内容转换为小写(通过将每个字符的值(ASCII VALUE)增加32),然后调用letterCount函数。 Or use strlwr to convert string to lower case. 或使用strlwr将字符串转换为小写。

You can use ascii values like this: count1[] for az and count2[] for AZ 您可以使用如下ascii值:az的count1 []和AZ的count2 []

if((s[c][i]  >= 97 && s[c][i] <= 122))
{
x = s[c][i] - 'a';
    count1[x]++;
}


if( (s[c][i] >= 65 && s[c][i]  <= 90)))
{
x = s[c][i] - 'A';
    count2[x]++;
}

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

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