How to read dynamically( any no of chars) from Keyboard using fgets...
I am new to fgets.. Help me please......
I tried ... where i have to make change...
Code:
int main()
{
char *str,*str1;int i=1;
printf("Enter the string");
str=(char *) malloc(sizeof(char)*40);
str1=str;
fgets(str,40,stdin);
printf("%s",str1);
while (str[strlen(str) - 1] != '\n')
{
++i;
str=(char *)realloc(str,sizeof(char)*40*i);
fgets(str,40,stdin);
printf("%s",str1);
}
printf("\n Now String contains %s",str1); // contains only last 40 chars..
system("pause");
}