Question

In: Computer Science

C program only There is a documented prototype for a function called get_a_line in the .c...

C program only

There is a documented prototype for a function called get_a_line in the .c file. Write a definition for get_a_line—the only function called from that definition should be fgetc. You can use the given main function to test your code. You should run the executable several times to check different cases:

  • Type Ctrl-D in response to the prompt for input. get_a_line should fail to read any characters, put a ’\0’ character at the beginning of the array, and return EOF.

  • Type abc and then Ctrl-D twice in response to the prompt for input. That input generates a failure after ’a’, ’b’, and ’c’ have been read.

  • Type abc and then Enter in response to the prompt for input. In that situation, all three letters followed by a newline should get into the array.

  • Type abcdefgh and then Enter in response to the prompt for input. In that situation, all eight letters followed by a newline should get into the array.

  • Type abcdefghi and then Enter in response to the prompt for input. In that situation, all nine letters should get into the array, but the newline won’t.

  • Type abcdefghijklmnop and then Enter in response to the prompt for input. In that situation, the first nine letters get into the array but not the other letters or a newline.

  • Hint: Writing get_a_line requires a lot of thought, but not a lot of code. It can be done in 20 or fewer rather short lines of code.

C Code:

// test-get_a_line4D.c

#include <stdio.h>
#include <string.h>

// Again, in a practical program, this is a ridiculously small size
// for an array that is supposed to hold a line of text. But it's
// convenient for testing purposes.
#define BUFFER_ARRAY_SIZE 10

int get_a_line(char *s, int size, FILE *stream);
// Does what fgets does, using repeated calls to fgetc, but
// provides a more useful return value than fgets does.
//
// REQUIRES
// size > 1.
// s points to the start of an array of at least size bytes.
// stream is open for input.
// PROMISES
// Return value is EOF if input error occurred.
// Otherwise, return value gives the index of the '\0' that
// terminates the string in the array.


int main(void)
{
char buffer[BUFFER_ARRAY_SIZE];
int retval;
printf("Enter a line of text:\n");
retval = get_a_line(buffer, BUFFER_ARRAY_SIZE, stdin);
printf("\nString in buffer is \"\%s\"\n", buffer);
if (retval == EOF)
printf("Return value from get_a_line was EOF.\n");
else
printf("Return value from get_a_line was %d.\n", retval);

return 0;
}

Solutions

Expert Solution

code in c (code to copy)

#include <stdio.h>
#include <string.h>

// Again, in a practical program, this is a ridiculously small size
// for an array that is supposed to hold a line of text. But it's
// convenient for testing purposes.
#define BUFFER_ARRAY_SIZE 10

int get_a_line(char *s, int size, FILE *stream);
// Does what fgets does, using repeated calls to fgetc, but
// provides a more useful return value than fgets does.
//
// REQUIRES
// size > 1.
// s points to the start of an array of at least size bytes.
// stream is open for input.
// PROMISES
// Return value is EOF if input error occurred.
// Otherwise, return value gives the index of the '\0' that
// terminates the string in the array.


int main(void)
{
char buffer[BUFFER_ARRAY_SIZE];
int retval;
printf("Enter a line of text:\n");
retval = get_a_line(buffer, BUFFER_ARRAY_SIZE, stdin);
printf("\nString in buffer is \"\%s\"\n", buffer);
if (retval == EOF)
printf("Return value from get_a_line was EOF.\n");
else
printf("Return value from get_a_line was %d.\n", retval);

return 0;
}

int get_a_line(char *s, int size, FILE *stream){
        // declare a counter for the s string and initialize with 0
        int k=0;
        //read first character
        char ch = fgetc(stream);
        //check if valid line
        if(ch==EOF||ch=='\n'){
                s[0]='\0';
                return EOF;
        }
        //loop until end of line
        while(k<size-1&&ch!=EOF&&ch!='\n'){
                s[k++]=ch;
                ch = fgetc(stream);
        }
        s[k]='\0';
        return k;
}

code screenshot

output screenshot #1

output screenshot #2


Related Solutions

(C++ only please) Write a function called maximum that takes an array of double values as...
(C++ only please) Write a function called maximum that takes an array of double values as a parameter and returns the largest value in the array. The length of the array will also be passed as a parameter. (Note that the contents of the array should NOT be modified.) Write a function called printReverse that takes an array of characters and the length of the array as parameters. It should print the elements of the array in reverse order. The...
Only Program in C for this. No other programming language is allowed. Using a function, we...
Only Program in C for this. No other programming language is allowed. Using a function, we will add a range of values of an array. The range is going to be determined by the user. In this example, if you put the following array down as: 1.5 -5.6 8.9 4.6 7.8 995.1 45.1 -5964.2 … and the user tells you to add from the 3rd element to the 6th element, your program is going to need to add the values:...
1.A production function such as Y=AKaLb is called the Cobb-Douglas (C-D) production function. Only two factors...
1.A production function such as Y=AKaLb is called the Cobb-Douglas (C-D) production function. Only two factors of production are assumed here: capital (K) and labor (L), with A interpreted as the level of technology. Later, it was discovered that human capital (H) is also a "neglected"factor of production, assuming that the function satisfies constant return to scales for all the factors of production that it should include (except for technology). In the correct production function, there should be: A. a+b=1。...
Programming in C (not C++) Write the function definition for a function called CompareNum that takes...
Programming in C (not C++) Write the function definition for a function called CompareNum that takes one doyble argument called "num". The function will declare, ask, and get another double from the user. Compare the double entered by the user to "num" and return a 0 if they are the same, a -1 num is less than the double entered by the user and 1 if it is greater.
Write a program that utilizes a function called paymentCalc. In the main body of the program,...
Write a program that utilizes a function called paymentCalc. In the main body of the program, ask the user what is their principal and how many months they want the loan. If it is easier, ask the user for number of years, but don’t forget to change it to months for the calculation. Use a function to calculate their monthly mortgage payment. In the main body of the program, print out what their monthly mortgage payment will be. For simplicity,...
C++ PLEASE Write a new program called Lab15A Write a void function named fillArray that receives...
C++ PLEASE Write a new program called Lab15A Write a void function named fillArray that receives an array of 10 integers as a parameter and fills the array by reading the values from a text file (Lab15A.txt). It should also print the values of the array all on the same line, separated by spaces. Write an int function named findLast that receives an array of 10 integers as a parameter and returns the last negative number in the array. Write...
In C Write a main function with a function call to a function called GetLetter which...
In C Write a main function with a function call to a function called GetLetter which has two double arguments/parameters. The function returns a character. Declare and initialize the necessary data variables and assign values needed to make it executable and to prevent a loss of information
In C++ Prototype your functions above "main" and define them below "main"; Write a program that...
In C++ Prototype your functions above "main" and define them below "main"; Write a program that uses two identical arrays of at least 20 integers. It should call a function that uses the bubble sort algorithm to sort one of the arrays in ascending order. The function should keep count of the number of exchanges it makes. The program then should call a function that uses the selection sort algorithm to sort the other arrays. It should also keep count...
( C language only )Generate a function  called randomnum(n,m,neg); where n and m are the lower and...
( C language only )Generate a function  called randomnum(n,m,neg); where n and m are the lower and upper bounds for the random number; the generated number is negative if a variable named neg is true .These numbers should be non-zero with a maximum absolute value of 15 . You must use bitwise arithmetic to calculate the modulus of the random numbers
( C language only and all values should be float )Generate a function called randomnum(n,m,neg); where...
( C language only and all values should be float )Generate a function called randomnum(n,m,neg); where n and m are the lower and upper bounds for the random number; the generated number is negative if a variable named neg is true .These numbers should be non-zero with a maximum absolute value of 15 . You must use bitwise arithmetic to calculate the modulus of the random number
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT