Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8304547
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T17:52:43+00:00 2026-06-08T17:52:43+00:00

This is a very fun problem I am running into. I did a lot

  • 0

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
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T17:52:44+00:00Added an answer on June 8, 2026 at 5:52 pm

    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: ");
            // ...
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is a very high-level question. I'm looking for insight into this problem that
I've had this problem several times before, so please excuse my bluntness. I cannot
This is my first time trying Rcpp and this very simple problem is giving
This very likely a setup issue on my part. I am trying to run
This very simple code: #include <iostream> using namespace std; void exec(char* option) { cout
This very strange. When I resize a WinForms dialog the controls are flickering very
Given this very familiar model of prototypal construction: function Rectangle(w,h) { this.width = w;
In this very simple code example, messages get lost every once in a while.
I'm using this very handy plugin to sort elements. But comparing elements on a
I have this very simple jQuery function: $(.milestone-in-tree).live({ mouseenter: function() { setTimeout( $.ajax({ type:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.