My goal is to make a program that would scan a word from input and then save it into one large string.
I know for sure that input is always word '\n'
word '\n'
...
So I'm trying to scan a single character and save it into an array and replace '\n'
with ' '
.
My code:
char c;
char *line;
int len = 0;
while(!feof(stdin))
{
len++;
char *line = (char *)malloc(sizeof(char) * len);
scanf("%c", &c);
if (c == '\n')
line[len - 1] = ' ';
else
line[len - 1] = c;
}
int q;
for(q = 0; q < len - 1; q++)
printf("%c", line[q]);
Ouput is error. (RUN FAILED (exit value 1, total time: 2s
)
For example I want for input :
one
two
three
four
five
and this for output:
"one two three four five"