home / study / questions and answers / engineering / computer science / in this
ID: 3668484 • Letter: H
Question
home / study / questions and answers / engineering / computer science / in this program, you will read a string from stdin, ... Question In this program, you will read a string from stdin, store it in an array, and print it back to the user. Your program will also print the size of the string, and the number of times it had to increase its buffer (A buffer is just an array used to temporarily store input.). The string ends when a newline or EOF is reached. ***Note*** - This question has already been asked here https://www.chegg.com/homework-help/questions-and-answers/c-programming-language-program-program-read-string-stdin-store-array-print-back-user-progr-q10504797 However, the answer is wrong... You may not assume that the input string is smaller than some fixed length. Use a buffer, keep track of the number of characters in the buffer along with its maximum size, and make a larger buffer when you run out of space. You may assume memory always has room for your buffer to increase (this is a bad assumption to make, in general). Constraints: Use the macro-defined constant INITIAL_BUFFER_SIZE to define your initial buffer size. Start with 16 characters. That is, your code needs to contain the line #define INITIAL_BUFFER_SIZE 16 Define the following function, and comment as indicated: char* IncreaseBuffer(char* buffer, int* buffer_size); This function doubles the capacity of your buffer by creating a new one and cleaning up the old one. IncreaseBuffer returns the new buffer to the caller. IncreaseBuffer doubles buffer_size. It must call either realloc or malloc and free. Above the function, comment as follows: /* * Input: describe the parameters of the function * Output: describe the variable being returned * Summary: … brief summary of function … */ Use getchar to read the string. You may not use any other function to read input from stdin. Use a single printf statement with %s in the format string to print the final string. You may not use any other function to print. Be sure to comment (this is part of your 10 points listed above) your code. You may not use string.h for this problem. Libraries used: ctype.h stdlib.h stdio.h Output example: [prompt]$ ./get_string Enter a string: The quick brown fox jumps over the lazy dog String size: 43 Buffer increases: 2 You entered: The quick brown fox jumps over the lazy dog [prompt]$ ./get_string Enter a string: No, you enter a string! String size: 23 Buffer increases: 1 You entered: No, you enter a string!
Explanation / Answer
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
char * read_string(void)
{
int buff_size = 11; /* size of temporary buffer */
char temp[11]; /* temporary input buffer */
char *output; /* pointer to allocated memory */
char *copy; /* copy of input string to be returned */
char *iterator; /* pointer to beginning of an array, used when searching for in input string */
output = malloc(10*sizeof(char)); /* allocates 10 bytes for first part of input */
if(output == NULL)
return NULL;
size_t size = 10*sizeof(char); /* keeps track of actual size of output */
//*output = ''; /* places NUL at the beginning of output, to make it empty string */
while(fgets(temp, buff_size,stdin) != NULL) /* read max 10 chars from input to temp */
{
strcat(output,temp); /* append read input to output, without char */
if(strlen(temp) != 10 || temp[9] == ' ') /* if buffer wasn't full or last char was -stop because it reached the end of input string */
break;
size += buff_size - 1; /* if didn' reach end of input increase size of output*/
output = realloc(output,size);
if(output == NULL)
{
return NULL;
}
}
iterator = output; /* search for in output and replace it with */
while(*iterator != ' ')
iterator++;
*iterator='';
copy=malloc(strlen(output) + 1); /* allocate memory for copy of output +1 for char */
if(copy == NULL)
return NULL;
strcpy(copy, output); /* strcpy output to copy */
free(output); /* free memory taken by output */
return copy;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.