QuestionQuestion

Transcribed TextTranscribed Text

Assignment Write a function that takes a buffer, a format string and three pointers to values and composes a string with the same text that would be printed if format was used on printf. The function should return the number of bytes written in to the buffer, if the number of placeholders is bigger than 3 return - -1, if its less use only the arguments needed. Assumptions 1. The buffer is big enough to contain the formatted string. prototype int format_to_string(char *buffer, const char *format, void *arg1, void *arg2, void *arg3); Supports Your function must support the following placeholders, 1. %c - interpret the argument as char and add the char to the buffer. 2. %d - interpret the argument as int and add the string representation of the int to the buffer. 3. %s - interpret the argument as char * and add the string to the buffer. 4. %x - interpret the argument as int * and add the string representation of the int as hexadecimal characters to the buffer. 1 5. %f - interpret the argument as float and add the string representation of the float with a precision of at most 7 digits with no trailing zeros, i.e. 1/4 should be added as 0.25 and 1/3 as 0. . 3333333. 6. %% - prints the symbol '%'. Theoretical question Write the answers to the following question as documentation to your function 1. Mention two format strings that your code is not handling correctly. 2. What is the behavior of the function for these format strings. Make sure your code does not crash for the format strings you mentioned.

Solution PreviewSolution Preview

This material may consist of step-by-step explanations on how to solve a problem or examples of proper writing, including the use of citations, references, bibliographies, and formatting. This material is made available for the sole purpose of studying and learning - misuse is strictly forbidden.

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

#define MAX_SIZE (256)

#define ASSERT_EQUAL(x,y,msg) do {\
    if ((x) != (y)) {\
       printf("%s - Failed\n", msg);\
       printf("Expected %d bytes and received %d\n", (x), (y));\
    } else {\
       printf("%s - Passed\n", msg);\
    }\
} while(0)

#define ASSERT_STRING_EQUAL(x,y,msg) do {\
    if (strncmp((x), (y), MAX_SIZE) != 0) { \
       printf("%s - Failed\n", msg); \
       printf("Expected:\n%s\nOutput:\n%s\n", (x), (y));\
    } else {\
       printf("%s - Passed\n", msg);\
    }\
} while(0)

int format_to_string(char *buffer,const char *format,void *arg1,void *arg2,void *arg3);

int main(void)
{

    char buffer[MAX_SIZE] = {0};
    char test_buffer[MAX_SIZE] = {0};
    int num_bytes = 0;
    int test_num_bytes = 0;

    int arg11 = 1;
    char arg12 = 'd';
    char *arg13 = "Hello";

    num_bytes = sprintf(buffer, "arg1 = %d, arg2 = %c, arg3 = %s\n", arg11, arg12, arg13);
    test_num_bytes = format_to_string(test_buffer, "arg1 = %d, arg2 = %c, arg3 = %s\n", &arg11, &arg12, arg13);

    ASSERT_EQUAL(num_bytes, test_num_bytes, "Test1");
    ASSERT_STRING_EQUAL(buffer, test_buffer, "Test2");

    int arg21 = 33;
    char *arg22 = "Hello";
    int arg23 = 43;

    memset(buffer, 0, MAX_SIZE);
    memset(test_buffer, 0, MAX_SIZE);

    num_bytes = sprintf(buffer, "%% arg1 = %d, arg2 = %s, %% arg3 = %x\n", arg21, arg22, arg23);
    test_num_bytes = format_to_string(test_buffer, "%% arg1 = %d, arg2 = %s, %% arg3 = %x\n", &arg21, arg22, &arg23);

    ASSERT_EQUAL(num_bytes, test_num_bytes, "Test3");
    ASSERT_STRING_EQUAL(buffer, test_buffer, "Test4");

    float arg31 = 1.0/3;
    float arg32 = 5.0/4;

    memset(buffer, 0, MAX_SIZE);
    memset(test_buffer, 0, MAX_SIZE);

    strcpy(buffer, "% arg1 = 0.3333333, arg2 = 1.25\n");
    num_bytes = strlen(buffer);
    test_num_bytes = format_to_string(test_buffer, "%% arg1 = %f, arg2 = %f\n", &arg31, &arg32, NULL);

    ASSERT_EQUAL(num_bytes, test_num_bytes, "Test5");
    ASSERT_STRING_EQUAL(buffer, test_buffer, "Test6");

    int arg41 = 33;
    char *arg42 = "Hello";
    int arg43 = 43;

    test_num_bytes = format_to_string(test_buffer, "arg1 = %d, arg2 = %s, arg3 = %d %c\n", &arg41, arg42, &arg43);
    ASSERT_EQUAL(test_num_bytes...
$50.00 for this solution

PayPal, G Pay, ApplePay, Amazon Pay, and all major credit cards accepted.

Find A Tutor

View available C-Family Programming Tutors

Get College Homework Help.

Are you sure you don't want to upload any files?

Fast tutor response requires as much info as possible.

Decision:
Upload a file
Continue without uploading

SUBMIT YOUR HOMEWORK
We couldn't find that subject.
Please select the best match from the list below.

We'll send you an email right away. If it's not in your inbox, check your spam folder.

  • 1
  • 2
  • 3
Live Chats