2

This is a very fun problem I am running into. I did a lot of searching on stack overflow and found others had some similar problems. So I wrote my code accordingly. I originally had fscan() and strcmp(), but that completely bombed on me. So other posts suggested fgets() and strncmp() and using the length to compare them.

I tried to debug what I was doing by printing out the size of my two strings. I thought, maybe they have /n floating in there or something and messing it up (another post talked about that, but I don't think that is happening here). So if the size is the same, the limit for strncmp() should be the same. Right? Just to make sure they are supposedly being compared right. Now, I know that if the strings are the same, it returns 0 otherwise a negative with strncmp(). But it's not working.

Here is the output I am getting:

perk
repk
Enter your guess: perk
Word size: 8 and Guess size: 8
Your guess is wrong
Enter your guess: 

Here is my code:

void guess(char *word, char *jumbleWord)
{
        size_t wordLen = strlen(word);
        size_t guessLen; 
        printf("word is: %s\n",word);
        printf("jumble is: %s\n", jumbleWord);


        char *guess = malloc(sizeof(char) * (MAX_WORD_LENGTH + 1));
        do
        {
            printf("Enter your guess: ");
            fgets(guess, MAX_WORD_LENGTH, stdin);
            printf("\nword: -%s- and guess: -%s-", word, guess); 
            guessLen = strlen(guess);
            //int size1 = strlen(word);
            //int size2 = strlen(guess); 

            //printf("Word size: %d and Guess size: %d\n",size1,size2);


            if(strncmp(guess,word,wordLen) == 0)
            {
                printf("Your guess is correct\n"); 
                break; 
            }

            }while(1);
    }

I updated it from suggestions below. Especially after learning the difference between char * as a pointer and referring to something as a string. However, it's still giving me the same error.

Please note that MAX_WORD_LENGTH is a define statement used at the top of my program as

#define MAX_WORD_LENGTH 25
GeekyOmega
  • 1,235
  • 6
  • 16
  • 34
  • I put in something called *jumbleword. Don't worry about it. I use it later, but omit it to the nuts and bolts for my problem here. – GeekyOmega Jul 31 '12 at 19:29
  • +1 for nice question formulation ;) but you should use `strlen` instead of `sizeof`, sizeof returns only size of type or fixed length structure. – Vyktor Jul 31 '12 at 19:34
  • fgets reads a line, so your `guess` buffer will contain a `\n` character. How does the word you're comparing against look like, where does it come from ? Does it contain a newline ? (You could print it out with printf("Comparing '%s' and '%s'\n", word, guess); That way you'll see if there's some whitespace or junk inbetween the apostrophes. – nos Jul 31 '12 at 21:21
  • 1
    Updated my answer for the off by one char problem you've got. – Spencer Rathbun Aug 01 '12 at 13:38
  • I updated my code to reflect debugging and input from forum users. Thank you. :-) – GeekyOmega Aug 01 '12 at 19:10
  • I consider this question closed. I thank all of you for your help and tried to give +rep to you all for helping out WAY beyond the call of duty in helping me code better. I am learning very fast. I admit I got stuck hours on this, but making progress again. And it was so simple!?! Derp on myself. Ha ha ha ha. I would buy you all beer if I knew you in person. – GeekyOmega Aug 01 '12 at 21:34

4 Answers4

4

Use strlen, not sizeof. Also, you shouldn't use strncmp here, if your guess is a prefix of the word it will mistakenly report a match. Use strcmp.

Keith Randall
  • 22,985
  • 2
  • 35
  • 54
2

sizeof(guess) is returning the size of a char * not the length of the string guess. Your problem is that you're using sizeof to manage string lengths. C has a function for string length: strlen.

sizeof is used to determine the size of data types and arrays. sizeof only works for strings in one very specific case - I won't go into that here - but even then, always use strlen to work with string lengths.

You'll want to decide how many characters you'll allow for your words. This is a property of your game, i.e. words in the game are never more that 11 characters long.

So:

// define this somewhere, a header, or near top of your file
#define MAX_WORD_LENGTH 11

// ...

size_t wordlen = strlen(word);
size_t guessLen;

// MAX_WORD_LENGTH + 1, 1 more for the null-terminator:
char *guess = malloc(sizeof(char) * (MAX_WORD_LENGTH + 1));

printf("Enter your guess: ");
fgets(guess, MAX_WORD_LENGTH, stdin);

guessLen = strlen(guess);

Also review the docs for fgets and note that the newline character is retained in the input, so you'll need to account for that if you want to compare the two words. One quick fix for this is to only compare up to the length of word, and not the length of guess, so: if( strncmp(guess, word, wordLen) == 0). The problem with this quick fix is that it will pass invalid inputs, i.e. if word is eject, and guess is ejection, the comparison will pass.

Finally, there's no reason to allocate memory for a new guess in each iteration of the loop, just use the string that you've already allocated. You could change your function setup to:

char guess(char *word, char *jumbledWord)
{
    int exit;

    size_t wordLen = strlen(word);
    size_t guessLen; 

    char *guess = malloc(sizeof(char) * (MAX_WORD_LENGTH + 1));

    do
    {
        printf("Enter your guess: ");
        // ...
pb2q
  • 58,613
  • 19
  • 146
  • 147
  • I think in this case wordLen is MAX_WORD_LENGTH unless that is a special reserved word? I googled it and found nothing. So that is what I assume you meant. – GeekyOmega Jul 31 '12 at 19:56
  • sorry, `MAX_WORD_LENGTH` is something that **you'll** `#define`. added to the answer. it's a constant that you'll choose, and it will limit all strings in the game to a max size. this way the string lengths will be easier to deal with, and since this is a game, it isn't unreasonable to choose a low upper bound; but it's your choice: 11, 23, 50, ... – pb2q Jul 31 '12 at 19:59
  • Thanks for your reply pb2q! I made the suggested changes and still getting the same error. It doesn't seem to care what I enter in. It never registers when the string is the same. I also replace guessLen trying in strncmp() and it doesn't work. I tried strcmp() too. – GeekyOmega Jul 31 '12 at 20:13
  • what inputs fail? what is `guess`? and word is `perk`? – pb2q Jul 31 '12 at 20:16
  • Here is what I meant by input: eject tcjee Enter your guess: eject Your guess is wrong Enter your guess: I edited the code in my question to reflect what I currently have. :-( I am hacking at it, and reading the strcmp() and strncmp() literature again, but not getting far. – GeekyOmega Jul 31 '12 at 20:19
  • Sorry pb2q, the formatting for comments isn't really good. I gave you console output showing the same problem. Which is you are shown the right word, the jumbled word, and then I guess it correctly. It tells me I am wrong. – GeekyOmega Jul 31 '12 at 20:22
  • your `strcmp` is failing because of the newline from `fgets` to convince yourself of this, add a `println` after the `fgets`: `printf("WORD: -%s-; GUESS: -%s-\n", word, guess);`. Note the dashes around the words, you should see a newline before the final dash. With the newline as part of the string, the comparison will break. – pb2q Jul 31 '12 at 20:28
  • I got it working! There was weird \r and \n stuff floating around. I made a function to get rid of that in all my strings and clean them. Then I compare them, and YES. Thank you SO MUCH pb2q. You are l33t in my book. Thanks for the patience and the help! – GeekyOmega Aug 01 '12 at 21:38
2

As everyone else has stated, use strlen not sizeof. The reason this is happening though, is a fundamental concept of C that is different from Java.

Java does not give you access to pointers. Not only does C have pointers, but they are fundamental to the design of the language. If you don't understand and use pointers properly in C then things won't make sense, and you will have quite a bit of trouble.

So, in this case, sizeof is returning the size of the char * pointer, which is (usually) 4 or 8 bytes. What you want is the length of the data structure "at the other end" of the pointer. This is what strlen encapsulates for you.

If you didn't have strlen, you would need to dereference the pointer, then walk the string until you find the null byte marking the end.

i = 1;
while(*guess++) { i++ }

Afterwards, i will hold the length of your string.

Update:

Your code is fine, except for one minor detail. The docs for fgets note that it will keep the trailing newline char.

To fix this, add the following code in between the fgets and strncmp sections:

if ( guess[guessLen-1] == '\n' ) {
    guess[guessLen-1] = '\0'; 
}

That way the trailing newline, if any, gets removed and you are no longer off by one.

Spencer Rathbun
  • 14,510
  • 6
  • 54
  • 73
  • +rep for this post. Thank you very much. I am reading KK based from comments from other readers. I admit that pointers are really weird to me right now. I wish I learned C first and then went to Java...kind of put the cart in front of the horse. – GeekyOmega Jul 31 '12 at 19:57
  • Just one small adjustment, size of a `char*` is almost never 1 byte, but usually 4 or 8. – Jens Gustedt Jul 31 '12 at 20:11
  • @GeekyOmega Once you grok them, you'll wonder what you did without them. Read [this SO answer](http://stackoverflow.com/a/5754/724357), for a really good overview of how they work in plain english. – Spencer Rathbun Jul 31 '12 at 20:24
1

Some list of problems / advices for your code, much too long to fit in a comment:

  • your function returns a char which is strange. I don't see the logic and what is more important, you actually never return a value. Don't do that, it will bring you trouble
  • look into other control structures in C, in particular don't do your exit thing. First, exit in C is a function, which does what it says, it exits the program. Then there is a break statement to leave a loop.

A common idiom is

do {

   if (something) break;
} while(1)
  • you allocate a buffer in each iteration, but you never free it. this will give you big memory leaks, buffers that will be wasted and inaccessible to your code
  • your strncmp approach is only correct if the strings have the same length, so you'd have to test that first
Jens Gustedt
  • 76,821
  • 6
  • 102
  • 177
  • I left out the logic. I apologize. Yes, I probably will just return an int for a score or something later, not an int, but you are right. As for the loop structure you just taught me. I am still learning about buffers and code, I am reading KK's book about C right now. I'm changing my do/while statement from now on. Thank you! – GeekyOmega Jul 31 '12 at 20:25