0

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?

Art Badger
  • 310
  • 1
  • 7
Michael
  • 199
  • 2
  • 16
  • 1
    Please format question properly and stick to the actual language you are using. Thanks. – luk32 Dec 11 '14 at 18:24
  • 1
    can you show how the code is called? – thumbmunkeys Dec 11 '14 at 18:25
  • 5
    If you pass "Johnny" as an argument, you don't need `gets`, just remove that line completely. – Ishamael Dec 11 '14 at 18:27
  • `number=bnumber_of_repeating(word,c);` word is entered above, and character is entered above like this : `printf("enter a word"); scanf("%s",word); printf("enter a character"); scanf("%c",&c);` – Michael Dec 11 '14 at 18:27
  • @Michael Update the question itself. http://stackoverflow.com/posts/27429810/edit – 2501 Dec 11 '14 at 18:28
  • @Ishamael answered. THanks. yeah i dont need gets(word) if im passing the word already. – Michael Dec 11 '14 at 18:29
  • @Michael In you comment above you call bnumber_of_repeating(word,c) where as initially you declare number_of_repeating(word,c). Are you probably calling wrong method? – istovatis Dec 11 '14 at 18:31
  • 3
    Just [don't ever use `gets()`](http://stackoverflow.com/q/2843073/10077), even if you do need to get input. Use fgets()` or something else that's capable of being used safely. – Fred Larson Dec 11 '14 at 18:32
  • @FredLarson and when is the best situation to use `gets()` ? can you give some example. thanks – Michael Dec 11 '14 at 18:42
  • @Michael: There IS NO SITUATION in which you should use `gets()`. Never. Pretend it doesn't exist. – Fred Larson Dec 11 '14 at 18:46
  • @Michael There is no _direct_ substitute for `gets()`. Use alternatives like `fgets()` or `getline()`. They have slightly different approaches concerning buffers as compared to `gets()` which simply assumes there is enough space for all user input. – chux - Reinstate Monica Dec 11 '14 at 19:03
  • @chux is it okay to use `scanf("%s",string)` and then `strcpy()` instead of getline or fgets? – Michael Dec 11 '14 at 19:09
  • @Michael Not really. Same problems. Many folks, including myself, avoid `scanf()`. Further, `scanf("%s",string)` and `gets()` have different functionality. `scanf("%s",string)` only saves non-white-space input and `gets()` reads up to `'\n'`, including other white-space. I have a candidate `gets()` substitute at http://stackoverflow.com/a/27429750/2410359 Certainly many exists. – chux - Reinstate Monica Dec 11 '14 at 19:19

2 Answers2

1
#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;
}
Ankur
  • 3,584
  • 1
  • 24
  • 32
0

if you're passing the string in there is no reason to call gets(), that could be it or your types could be wrong.

peterjtk
  • 41
  • 1
  • 7