Reputation: 309
I have been reading "The C Programming Language" and I got to this problem, that my output is 0
for any given string I send.
My function looks like this:
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;
}
Problem:
I send him word for example: Jhonny
, and character n
, so it should count number of n's in the word (in this case the output should be 2).
What am I doing wrong?
Upvotes: 0
Views: 330
Reputation: 3724
#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;
}
Upvotes: 1
Reputation: 41
if you're passing the string in there is no reason to call gets(), that could be it or your types could be wrong.
Upvotes: 0