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

C programming language: Program: In this program, you will read a string from st

ID: 3668120 • 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. 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!

Explanation / Answer

This below c program is written based on the given problem conditions and statements matching.

1. Created additional memory based on the malloc
2. Compared and created additional memory
3. Read the string from console and print using getchar method in main.
4. See the below code to understand easily

#include<stdio.h>
#include<stdlib.h>
#include<ctype.h>

#define INITIAL_BUFFER_SIZE 16

// This function will create size of the buffer to double size
// it will use the below while loop to calculate the length of the buffer..
// If the length of the buffer is below or equal size to INITIAL_BUFFER_SIZE..It will double the size
// using malloc function and return the size.

char* IncreaseBuffer(char*buffer,int*buffer_size)
{
   int length = 0, i=0;
   while(buffer[i] !='')
   {
       length++;
       i++;
   }
   if(length == INITIAL_BUFFER_SIZE)
   *buffer_size =   malloc((INITIAL_BUFFER_SIZE * 2 ) * sizeof(char *));
   return buffer_size;
}

int main()
{
   char buffer[INITIAL_BUFFER_SIZE], c;
   int i = 0, count = 0, *size = 0, length=0;

   do{
       c = getchar();
       buffer[i] = c;
       i++;
       length++;
   }while(c != ' ' && c != EOF);

   printf("You Enetered String is %s ",buffer);

   //*size = IncreaseBuffer(buffer,length);
   return 1;
}