Question
C programming language: Program: In this program, you will read a string from stdin, store it in an array, and print it back to the
C programming language:
Program:
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. ONLY USE LIBRARIES: ctype.h, stdlib.h, & stdio.h
1. 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).
2. Constraints:
a. 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
home / study / questions and answers / engineering / computer science / c programming language: program: in this program, ...
Question
C programming language:
Program:
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. ONLY USE LIBRARIES: ctype.h, stdlib.h, & stdio.h
1. 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).
2. Constraints:
a. 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
#defineINITIAL_BUFFER_SIZE16
b. Define the following function, and comment as indicated:
char*IncreaseBuffer(char*buffer,int*buffer_size);
i. This function doubles the capacity of your buffer by creating a new
one and cleaning up the old one.
1. IncreaseBuffer returns the new buffer to the caller.
2. IncreaseBuffer doubles buffer_size. ii. It must call malloc and free. iii. Above the function, comment as follows:
/*
*Input:describetheparametersofthefunction
*Output:describethevariablebeingreturned
*Summary: briefsummaryoffunction
*/
c. Use getchar to read the string. You may not use any other function to read
input from stdin.
d. 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.
i.e.
[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!
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started