Could you explain following code?
str = (char *) malloc (sizeof(char) * (num+1));
malloc doing here?
This code tries to allocate a chunk of memory that can hold num + 1 values of type char. So if a chat equals one byte and num is 10 it will try to allocate 11 bytes of memory and return a pointer to that memory.
+1 is likely used because the programmer wanted to store a string (character array) of num characters and needs an extra char to store the terminating '\0' (null) character. In C/C++. chracater strings are, by convention, terminated by a null-character.