Solution Found!
Using the strncpy and strncat functions, implement a functionvoid safe_concat(const char
Chapter 7, Problem P7.9(choose chapter or problem)
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’);
}