I'was trying to make simple program that allows userCode:#include <stdio.h> #include <string.h> #include <stdlib.h> #define BUFSIZE 11 int main() { char buffer[BUFSIZE]; char *string; clrscr(); printf("Enter string: "); fgets(buffer,BUFSIZE,stdin); string=(char *)malloc(sizeof(char)*strlen(buffer)+1); strcpy(string,buffer); if((strchr(buffer,'\n'))!=NULL) puts(string); else{ do{ fgets(buffer,BUFSIZE,stdin); string=(char *)realloc(string,sizeof(char)*(strlen(string)+strlen(buffer))+1); strcat(string,buffer); } while((strchr(buffer,'\n'))==NULL); puts(string); } free(string); return 0; }
to enter string variable size with minimum wasting of memory.
So I figure out this, I wrote thisjust to test fgets() function, so I define BUFSZIE as 11.
Maybe this can be accomplished with less code, so if someone have some corection to suggest
welcome