Could you explain following code?
str = (char *) malloc (sizeof(char) * (num+1));
malloc doing here?
malloc allocates memory from the heap and returns a pointer to it. It's useful when you don't know how much memory you are going to need at compile time.
As for why (num+1), it really depends on what the code is doing... perhaps num is the number of characters in the string, and the +1 is for the NUL terminator byte at the end. I don't know what the sizeof(char) would be for in that case, though.