【发布时间】:2017-11-30 17:31:53
【问题描述】:
我有一个代码,我应该让它遍历名称列表并计算一个字母被使用了多少次。到目前为止,它适用于小写,但我如何在图片中实现大写。 非常感谢您的帮助。
/*
* 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++;
}
}
}
【问题讨论】:
-
我建议你看看
toupper()(或tolower())和isalpha()。 -
这个“也”指的是什么?
-
将难以阅读的
if (s[c][i] >= p && s[c][i] <= j )更改为if (s[c][i] >= 'a' && s[c][i] <= 'z')。然后使用if (s[c][i] >= 'A' && s[c][i] <= 'Z') { x = s[c][i] - 'A'; /* etc */重复该代码块 -
所以您希望
a和A都指向count[0]++?