Question: I wanted to empty the string:

```
char str[40]={"abcdefghijklmnopqrstuvwxyz"};

```

I first tried doing `strcpy(str2, "");` which seemed to help. In fact `printf("%s", str2);` gives a string of blank spaces. However when I fill up the first element of the array `str2[0]='r'`, it gives back the full string with the first character substituted.

I think that's because the `strcpy` function made the string look like this: `"\0bcdefghijklmnopqrstuvwxyz"`, and the printf stops at `'\0'`, so the string wasn't actually empty.

Searching on the internet I found the `memset()` function that actually solved the problem. I implemented it like this: `memset(str2, 0, strlen(str2));`

I then created a for cycle to try emulating what memset did to understand it better:

```
for(y=0; y<strlen(str2); y++)
{
    str2[y]=0;
}

```

But this led to the same issue of `strcpy()`, if I set `str2[y]='g';` the string appears to be still there when I print it. I tried to cast in the for cycle `str2[y]=(unsigned char)0;` because I read that memset converts the `int` parameter to `unsigned char`, but nothing changed. However I can't understand this. It's not like I'm filling only the first cell of the array like in the `strcpy` (assuming I was right in that).

So, what's happening?

Answer: 
OP's attempts have at least these problems:

**Loop does not execute more than once.**

> 
> I then tried creating a for cycle to try emulating what memset did to understand it better:
> 
> 
> 

```
for(y=0; y<strlen(str2); y++) {
  str2[y]=0;
}

```

After the first iteration, `strlen(str2)` is 0, so no more iterations.

---

**Questionable observation**

> 
> I first tried doing `strcpy(str2, "");` which seemed to help, in fact `printf("%s", str2);` gives a string of blank spaces.
> 
> 
> 

The output is not expect to be any spaces. No output is expected of a *string* that begins with a *null character*.
A *string* is a sequence of character values including a zero-valued terminator - the string `"hello"` s represented as the sequence `{'h', 'e', 'l', 'l', 'o', 0}`. Strings are stored in arrays of character type. To store an N-character string, then the array needs to be *at least* N+1 elements wide to account for the terminator.

You are correct that `strcpy` does not overwrite the contents of the array past what’s required by the new string. If I have

```
char str[10] = "abcdefghi";

```

and then do

```
strcpy(str, foo);

```

then the contents of `str` becomes `{'f', 'o', 'o', 0, 'e', 'f’, 'g', 'h', 'i', 0}`.

If you want to overwrite the entire array, then yes you either need to use `memset` or a loop. The problem with your loop is that as soon as you overwrite the first element of the array with `0` then `strlen(str)` returns `0` and your loop exits.

But beyond that `strlen` is the wrong tool to use anyway, since it doesn’t give you the size of the array, only the length of the string stored in the array. You either need to store the array size in a separate variable, or you need to use the `sizeof` operator:

```
for ( size_t i = 0; i < sizeof str; i++ )
  str[i] = 0;

```