I suppose easier way to do this is:
size_t len = strlen(buf); // will calculate number of non-0 symbols before first 0
char * newBuf = (char *)malloc(len); // allocate memory for new array, don't forget to free it later
memcpy(newBuf, buf, len); // copy data from old buf to new one
7
solved Remove trailing NULL terminator