C programming language: Program: In this program, you will read a string from st
ID: 3667070 • Letter: C
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.
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:
Libraries: ctype.hstdlib.hstdio.h
[prompt]$./get_string
Enterastring:Thequickbrownfoxjumpsoverthelazydog
Stringsize:43
Bufferincreases:2
Youentered:Thequickbrownfoxjumpsoverthelazydog
[prompt]$./get_string
Enterastring:No,youenterastring!
Stringsize:23
Bufferincreases:1
Youentered:No,youenterastring!
Explanation / Answer
Program plan:
In this program we need to create a function called increaseBuffer which is used to increase the buffer size which is already set.
Program:
#include<conio.h>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define INITIAL_BUFFER_SIZE 16
#define NULL 0
char* increasedBufferSize(char *buffer,int *buffer_size)
{
buffer=(char*)realloc(buffer,2*INITIAL_BUFFER_SIZE);
return buffer;
}
void main()
{
char *ch,a[50],*buffer;
/*allocating memory*/
if((ch=(char*)malloc(INITIAL_BUFFER_SIZE)))==NULL))
{printf("malloc failed");exit(1);}
printf("enter a string to store");
scanf("%s",a);
strcpy(ch,a);
if((ch=getchar(a)==EOF){buffer=increaseBufferSize(ch,3);
printf("the stored string is %s",a);
printff("size %d,strlen(a);
getch();
}
OUTPUT:
enter a string to store:
this program for increasing buffersize.
the stored string is :this program for increasing buffersize.
size 41
buffer increases 3
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.