Problem with string in C

Hello guys,

I try to create a little game and I have a problem I can't solve.
The objective is to hide a word by stars, one by letter, but when I try to show the hidden word, the console give me more characters than I expect.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <stdio.h>

int main()
{
    int word_size, n;
    char word[]="mytest";

    word_size = strlen(word);
    char hide_answer[word_size];
    for (n=0; n<word_size; n++)
    {
        hide_answer[n]='*';
    }
    printf("word = %s\n", word);
    printf("hide_answer = %s\n", hide_answer);

  return 0;

}


Answer
word = mytest
hide_answer = ******∟w♠
(approximately)

Thanks in advance to your answers.
Its wrong to initialize an array like this with dynamically changing variables.

char hide_answer[word_size];

You can use malloc operator to create and afterwards you can free it.

char *hide_answer=(char*)malloc(word_size*sizeof(char));

before return 0; you can add free(hide_answer);
It is perfectly fine to initialize an array that way in C, but what strlen() returns is not big enough to hold a C string. You need one more character to hold the terminating null:

char hide_answer[word_size+1];
..and you need to actually write it in there:
hide_answer[word_size] = '\0';

(also, don't forget to #include <string.h> for strlen)
Last edited on
Philtux,

Your problem is likely that hide_answer[] dose not have a '\0' terminating the char array. printf() will output characters until it finds a null character to stop at, hence the junk at the end of the line. Soo make your array large enough for the string and a null character ('\0') and add the null character to the end of the array.

Edit:
tvrameshmc wrote:
Its wrong to initialize an array like this with dynamically changing variables.
variable-length arrays are allowed in C99.
Last edited on
As
@Cubbi
pointed out already the new array shall have size equal to strlen( word ) + 1 where 1 is added to store the terminating zero.

In your particular example you could write much simpler

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <stdio.h>
#include <string.h>

int main( void )
{
    char word[] = "mytest";
    char hide_answer[sizeof( word )];

    memset( hide_answer, sizeof( hide_answer ) - 1, '*' );
    hide_answer[sizeof( hide_answer ) - 1] = '\0';

    printf("word = %s\n", word);
    printf("hide_answer = %s\n", hide_answer);

    return 0;
}


If you need to write a function when the size of the source character array is not known then it can look the following way

1
2
3
4
5
6
7
8
9
10
11
12
13
char * MakeHiddenWord( const char *s, char c )
{
   char *t = malloc( strlen( s ) + 1 );

   if ( t )
   {
      char *p = t;
      for ( ; *s; ++s ) *p++ = c;
      *p = '\0';
   }

   return ( t );
}


It can be called as

char *hide_answer = MakeHiddenWord( word, '*' );
Last edited on
The problems in the code
1. You did not include string.h for strlen()
2. You cannot declare hide_answer like that, check the way i did it
3. The last character of the string should be \0 as printf will print characters till it encounters a \0 in the memory.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <stdio.h>
#include <string.h>

int main()
{
    int word_size, n;
    char word[]="mytest";

    word_size = strlen(word);
    char* hide_answer = new char[word_size+1];
    for (n=0; n<word_size; n++)
    {
        hide_answer[n]='*';
    }
    hide_answer[word_size] = '\0';
    printf("word = %s\n", word);
    printf("hide_answer = %s\n", hide_answer);

    return 0;

}

Hope this helps.
Last edited on
Thanks for your help, i'll try differents answers and you'are all right.

Topic archived. No new replies allowed.