#include<stdio.h>
#include<stdlib.h>
#define MAX 5
/* to take a string input of unknown length from the user and print it*/
void main()
{
int i=0;
char *s;
s=(char *) malloc(MAX);
if(!s)
printf("memory not available\n"
int c;
while (( (c=getchar())!='\n') && (i < (MAX-1) ) )
s[i++]=c;
s='\0';
int j =MAX;
while((i==j-1))
{
j = j+MAX;
s=(char*) realloc(s,j);
if(!s)
printf("memory not available\n"
while(c!='\n' && (i < (j-1)))
{
s[i++]=c;
c=getchar();
}
s='\0';
}
printf("%s\n",s);
free(s);
}
#include<stdlib.h>
#define MAX 5
/* to take a string input of unknown length from the user and print it*/
void main()
{
int i=0;
char *s;
s=(char *) malloc(MAX);
if(!s)
printf("memory not available\n"
int c;
while (( (c=getchar())!='\n') && (i < (MAX-1) ) )
s[i++]=c;
s='\0';
int j =MAX;
while((i==j-1))
{
j = j+MAX;
s=(char*) realloc(s,j);
if(!s)
printf("memory not available\n"
while(c!='\n' && (i < (j-1)))
{
s[i++]=c;
c=getchar();
}
s='\0';
}
printf("%s\n",s);
free(s);
}