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

Implement a string processing function with the prototype char *my_strcat( const

ID: 3598060 • Letter: I

Question

Implement a string processing function with the prototype char *my_strcat( const char * const stri, const char const str2); The function creates a new string by concatenating str1 and str2. The function has to call malloc O or calloc to allocate memory for the new string, i.e., for the total number of characters, plus the null character. The function returns the new string (i.e., the value of a pointer to the first array element, in other words, the address of the first array element). You are not allowed to call functions declared in the standard library header , except for strlen C (strlen (s) returns the size of string S). After executing the following call to printfO: printf ( '%s ", my-strcat( "Hello", "world!" )); the printout on the screen has to be Helloworld! Write a program to test the function.

Explanation / Answer

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
char* my_strcat(const char* str1, const char* str2) {
int l1 = strlen(str1);
int l2 = strlen(str2);
char *str;
int i,j;
str=(char*)malloc((l1+l2+1)*sizeof(char));
for(i=0;i<l1;i++) {
str[i]=str1[i];
}
for(j=0;j<l2;j++,i++) {
str[i]=str2[j];
}
str[i]='';
return str;
}
int main()
{
printf("%s ", my_strcat("Hello","world!"));
return 0;
}

Output:

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote