I can't work out why Strchr()
is not working in my function. I need to see if the users guess matches any of the letters in a hidden word. It is a Hangman game.
int guessLetter(char* word, int* guessedLetters)
{
char guess[20];
char *s;
printf("Enter your guess: ");
scanf("%s", &guess);
s = strchr (word, guess);
printf("%s", s);
if (s != NULL) {
printf ("Good Guess\n");
} else {
printf ("Bad Guess\n");
}
}
No matter if the guess is right or wrong, my else
statement is being activated. My printf
shows that s
is being given the value of Null
no matter if the character is in the word or not.
So I guess my problem is with this part of the code:
s = strchr (word, guess);
I am new to C, so I am sure I am just missing something very basic. I have tried to search the web as much as I can, but I don't really seem to be able to understand what I am doing wrong.