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

2. Write a function called strcat406. The function is passed two parameters, bot

ID: 3819226 • Letter: 2

Question

2. Write a function called strcat406. The function is passed two parameters, both of which are C strings. You should use pointer syntax when writing this function (in other words, you can use * and &, but not [ ]). The function should modify the first parameter (a char array) so that it contains thee concatenation of the two strings, and return a pointer to the array. For example:

char str1[10] = "comp";

char str2[ ] = "uter";

char str3[10] = "comp";

strcat406(str1, str2);

printf("str1 contains %s str2 contains %s ", str1, str2);

printf("strcat406(s3, s2) returns %s ", strcat406(str3, str2));

The output of this code should be

str1 contains computer

str2 contains uter

strcat406(s3,s2) returns computer

Note that strcat406 is guaranteed to work properly only if str1's length is big enough to contain the concatenation of the two strings. In this case, "computer" takes up 9 bytes, and since str1 is 10 bytes, the function should run properly. On the other hand:

char str1[] = "comp"; // only 5 bytes

char str2[] = "uter";

strcat406(str1, str2); // takes up 9 bytes and // therefore overflows str1

Upon execution, a runtime error may occur, or (even worse) no runtime error will occur, but some other variable(s) in your program may be overwritten.

printf(" Problem 2 ");

strcpy(str1,"comp");
strcpy(str2,"uter");
printf("strcat406("%s",", str1);
printf(" "%s") = %s ", str2, strcat406(str1, str2));
strcpy(str2, " science");
strcat406(str1, str2);
printf("strcat406("computer", " science" = %s ", str1);
strcpy(small, "comp");
strcpy(str2, "uter systems I");
printf("before strcat small = %s str2 = %s ", small, str2);
printf("small array strcat406("comp","uter systems I") = ");
strcat406(small, str2);
printf(" "%s" ", small);
printf("str2 = "%s" ", str2);

Explanation / Answer

The below program gets required output

/*---------------------------------------------------------------------------
Program to concatenate two strings and return a pointer to the array
---------------------------------------------------------------------------*/
#include<stdio.h>
#include<conio.h>
#include<string.h>
char *strcat406(char *str1, char *str2);
void main()
{
// Char String Declaration
   char small[10];
   char str1[10] = "comp";
char str2[10] = "uter";
char str3[10]= "comp";

strcat406(str1, str2);
printf("str1 contains %s str2 contains %s ", str1, str2);
printf("strcat406(s3, s2) returns %s ", strcat406(str3, str2));
   printf(" Problem 2 ");
strcpy(str1,"comp");
strcpy(str2,"uter");
printf("strcat406("%s",", str1);
printf(" "%s") = %s ", str2, strcat406(str1, str2));
strcpy(str2, " science");
strcat406(str1, str2);
printf("strcat406("computer", " science" = %s ", str1);
strcpy(small, "comp");
strcpy(str2, "uter systems I");
printf("before strcat small = %s str2 = %s ", small, str2);
printf("small array strcat406("comp","uter systems I") = ");
strcat406(small, str2);
printf(" "%s" ", small);
printf("str2 = "%s" ", str2);
}
char *strcat406(char *str1, char *str2)
{
   int i = 0,len = 0;
   while(*(str1+len)!='')
       len++;
   while(*(str2+i)!='')
   {
       *(str1+len) = *(str2+i);
       i++;
       len++;
   }
   *(str1+len) = '';
   return str1;
}

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