Could you explain following code?
str = (char *) malloc (sizeof(char) * (num+1));
malloc
Malloc allocates memory, in this case for the string str of length num. (char *) is the type for str sizeof(char) is the number of bytes required for each character. The +1 is for the trailing null character in the string, normally zero.