Could you explain following code?
str = (char *) malloc (sizeof(char) * (num+1));
malloc
doing here?
Malloc is a call to allocate memory.
The above code is going to allocate space for num + 1 characters. Likely there is a string with num characters in, and the author of the code has added space for the null terminator.
After the call str will point to the start of that block of memory which has been allocated.