Solution Found!

Using the strncpy and strncat functions, implement a functionvoid safe_concat(const char

Chapter 7, Problem P7.9

(choose chapter or problem)

Get Unlimited Answers
QUESTION:

Using the strncpy and strncat functions, implement a functionvoid safe_concat(const char a[], const char b[], char result[], int result_maxlength)that concatenates the strings a and b to the buffer result. Be sure not to overrun thebuffer. It can hold result_maxlength characters, not counting the '\0' terminator.(That is, the buffer has result_maxlength + 1 bytes available.)

Questions & Answers

QUESTION:

Using the strncpy and strncat functions, implement a functionvoid safe_concat(const char a[], const char b[], char result[], int result_maxlength)that concatenates the strings a and b to the buffer result. Be sure not to overrun thebuffer. It can hold result_maxlength characters, not counting the '\0' terminator.(That is, the buffer has result_maxlength + 1 bytes available.)

ANSWER:

Step 1 of 3

#include <iostream>

#include <cstring>

using namespace std;

void concat(const char a[ ], const char b[ ], char result[ ], int result_maxlength);

int main()

{

    char a[] = "Woozle";

    char b[] = "Heffalump";

    char c[5];

    char d[10];

    char e[20];

    concat(a, b, c, 5);

    concat(a, b, d, 10);

    concat(a, b, e, 20);

    cout << c << "\n";

    cout << d << "\n";

    cout << e << "\n";

    return 0;

}

void concat(const char a[ ], const char b[ ], char result[ ], int result_maxlength)

{    

    strncat(result, a, result_maxlength = ‘\0’);

    strncat(result, b, result_maxlength = ‘\0’);

}

Add to cart


Study Tools You Might Need

Not The Solution You Need? Search for Your Answer Here:

×

Login

Login or Sign up for access to all of our study tools and educational content!

Forgot password?
Register Now

×

Register

Sign up for access to all content on our site!

Or login if you already have an account

×

Reset password

If you have an active account we’ll send you an e-mail for password recovery

Or login if you have your password back