我一直在阅读“ The C Programming Language”,但我遇到了这个问题,我的输出适用0
于我发送的任何给定字符串。
我的函数看起来像这样:
int number_of_repeating(char *word,char k){
int b=0,len=0,i;
gets(word);
len=strlen(word);
for(i=0;i<len;i++){
if(word[i]==k)
b++;
}
return b;
}
问题:
我给他发送了单词,例如Jhonny
和n
,所以它应该计算单词中n的个数(在这种情况下,输出应为2)。
我究竟做错了什么?
#include <stdio.h>
int number_of_repeating(char *word,char k){
int b=0,len=0,i;
gets(word); //<------- You need to remove this one because it may overwrite
len=strlen(word);
for(i=0;i<len;i++){
if(word[i]==k)
b++;
}
return b;
}
int main(void) {
// your code goes here
printf("%d",number_of_repeating("johnny",'n'));
return 0;
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句