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

C++ Implement the following two functions: -Invoke the function by passing the p

ID: 3588242 • Letter: C

Question

C++

Implement the following two functions:

-Invoke the function by passing the pointer of a string array, and return the size of the string array:

int count_char(constchar *)

-Invoke the function by passing the pointers of two string arrays. The content of the second string array will be appended to the end of the first string array:

void concatenate(char *, constchar *);

I am having trouble understanding how to use pointers correctly with functions, in this case, using them with string arrays and the given functions.

Here is my code so far without the two functions intcount_char(constchar *) and void concatenate(char *, constchar *);

_____________________

#include

#include

using namespace std;

int main()

{

string s1, s2, s3;

s1 = "Do you enjoy coding in C++?";

cout << "Enter the first string: ";

getline(cin, s2);

cout << "Enter the second string: ";

getline(cin, s3);

cout << "Word counts for string 1: ";

cout << "Word counts for string 1: " << s2.length() << endl;

cout << "Word counts for string 2: " << s3.length() << endl;

cout << "The first string: " << s2 << s3 << endl;

cout << "Word counts for string1:(after concantenation) " <

cout << "The second string: " << s3 << endl;

cout << "Word counts for string2:(after concantenation) " << s3.length() << endl;

}

_____________________

Here is my code again and what the output is supposed to look like:

COMP251C++ (Global Scope) main 4. 6 7 8 #include

Explanation / Answer

You can get character arrayb from string using string.c_str() function. Say you have s1 = "hello". you can get char array with s1,c_str().

#include<iostream>

using namespace std;

int count_char(const char *str){
   int i;
   for (i = 0; *(str+i) != ''; i++);
      
   return i;
}

void concatenate(char *str1, const char *str2){
  
   int count1 = count_char(str1);
   int count2 = count_char(str2);
   int i,j;

   char *res = new char[count1 + count2 + 1];
   for (i = 0; *(str1+i) != ''; i++){
       res[i] = *(str1+i);
   }
   for (j = 0; *(str2+j) != ''; j++){
       res[i+j] = *(str2+j);
   }
   res[count1+count2+1] = '';
   cout << res << endl;

}

int main(){

   char str[] = "Hello";
   char str1[] = "my length is 5";
   cout << "length :" << count_char(str) << endl;
   concatenate(str,str1);
   return 0;
}