Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Book Comouter systems 3rd edition pro 3.71 Write a function good_echo that reads

ID: 3932984 • Letter: B

Question

Book Comouter systems 3rd edition pro 3.71

Write a function good_echo that reads a line from standard input and writes it to standard output. Your implementation should work for an input line of arbitrary length. You may use the library function fgets, but you must make sure your function works correctly even when the input line requires more space than you have allocated for your buffer. Your code should also check for error conditions and return when one is encountered. Refer to the definitions of the standard I/O functions for documentation [45, 61].

Explanation / Answer

Here is the code for you:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
void good_echo()
{
printf("Enter the line of text to read: ");
int size = 10   ;
char *temp = malloc(sizeof(size));
char *line = malloc(sizeof(size));
while(1)
{
fgets(temp, size, stdin);
if(temp[strlen(temp)-1] == ' ')
{
strcat(line, temp);
printf("%s ", line);
exit(0);
}
  
line = malloc(sizeof(size*2));
size *= 2;
strcat(line, temp);
}
}
int main()
{
good_echo();
}