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

IN C++ Concatenating arrays Strings have build-in functions to perform concatena

ID: 3688851 • Letter: I

Question

IN C++

Concatenating arrays Strings have build-in functions to perform concatenation. Write a function called append () that performs concatenation for two char arrays (you will have to pass in a third char array that will hold the result). You will also need to pass in the lengths of each char array. You may not use strings for this part. Sample main code: char first[ ] = {'I', ' ', 'a', 'm', ' '}; char second[ ] = {'i', 'r', 'o', 'n', 'm', 'a', 'n', ''}; char result[200]; append(first, 5, second, 8, result); cout

Explanation / Answer

Hi, Please find below code, revert me back if this is not what you want to see.

#include <iostream>
#include <string>
using namespace std;

void append(char a[], int lena,char b[],int lenb, char* c)
{
int lenc=lena+lenb;
int i=0,j=0;
for(i;i<lena;i++,j++) // copies all char from a to c
{
   c[j]=a[i];
  
}
for(i=0;i<lenb;i++,j++) //Copies all chars for b to c
{
   c[j]=b[i];
}
  
c[lenc]=''; // marking end of the string
   
}

int main ()
{
char a[] = {'s','a','i',' '};
char b[]={'m','u','r','a','l','i'};
int lena=sizeof(a);
int lenb=sizeof(b);
int lenc=lena+lenb;
char c[lenc+1]; // extra 1 length for holidng end of string char
append(a,lena,b,lenb,c);
  
cout << c;

return 0;
}

open below link to execute the code.

http://ideone.com/ego1Ik

Thanks,