Question

In: Computer Science

C Implement the function Append (char** s1, char** s2) that appends the second character array to...

C

Implement the function Append (char** s1, char** s2) that appends the second character array to the first, replaces the first array with the result and replaces the second character array with the original first array. For example, if the first character array is "hello" and the second is "world" at the end of the function the new value of the first character array should be"helloworld" and the second array should be "hello". If the input is invalid the function should return 1. Otherwise, the function should return 0.

Input is double pointer (**). No use of <string.h>, USE malloc

#include <stdlib.h>

#include <stdio.h>

int

Append(char **s1, char **s2){

//implement

}

int main(){

char myArray1[7] = "hello";

char myArray2[10] = "world";

char *myArray3 = myArray1;

char *myArray4 = myArray2;

Append(&myArray3, &myArray4);

return 0;

}

Solutions

Expert Solution

Description:

Complete program which appends the second array to first array and assign first array to second array. To achieve the goal, memory is allocated first which can hold the appended array and another memory is allocated for holding first array. After allocating the memory, first the s1 is copied into newly allocated memory and then second array is concatenated/appended. null character '\0' inserted at the end of the result string.

Now s1 is copied into the another allocated memory tempS2. null character '\0' inserted at the end of the result string.

Now we can assign s1 and s2 the new values.

Complete program which you can copy:

#include <stdlib.h>

#include <stdio.h>
#include <string.h>

int Append(char **s1, char **s2) {
/* get the length of the strings as will be needed
in allocating memory for new strings*/
int s1Length = strlen(*s1);
int s2Length = strlen(*s2);
char *tempS2 = NULL, *tempS1 = NULL;

/* return 1 if s1 or s2 string is null */
if (*s1 == NULL && *s2 == NULL) {
return 1;
}
  
/* Allocate memory which can hold concatenated string */
tempS1 = (char*)malloc(s1Length + s2Length + 1);
if (tempS1) {
/* copy first array s1 first and then concatenate second array s2 */
strcpy(tempS1, *s1);
strcat(tempS1, *s2);
/* insert null charcter at end of the string */
tempS1[s1Length + s2Length] = '\0';
}

/* Allocate memory which can hold concatenated string */
tempS2 = (char*)malloc(s1Length + 1);
if (tempS2) {
/* copy first array s1 to placeholder for s2 */
strcpy(tempS2, *s1);
/* insert null charcter at end of the string */
tempS2[s1Length] = '\0';
}

/* assign tempS1 to s1 (concatenated string) and tempS2 to s2 (s1 value)*/
*s1 = tempS1;
*s2 = tempS2;

return 0;
}

int main() {
char myArray1[7] = "hello";
char myArray2[10] = "world";

char *myArray3 = myArray1;
char *myArray4 = myArray2;

Append(&myArray3, &myArray4);

printf("myArray3: %s\n", myArray3);
printf("myArray4: %s", myArray4);

return 0;
}

Formatted program:

Sample output:


Related Solutions

C Write a function that appends the second character array to the first, replaces the first...
C Write a function that appends the second character array to the first, replaces the first array with the result and replaces the second character array with the original first array. For example, if the first character array is "hello" and the second is "world" at the end of the function the new value of the first character array should be"helloworld" and the second array should be "hello". If the input is invalid the function should return 1. Otherwise, the...
Program in C: The strncpy(s1,s2,n) function copies exactly n characters from s2 to s1, truncating s2...
Program in C: The strncpy(s1,s2,n) function copies exactly n characters from s2 to s1, truncating s2 or padding it with extra null characters as necessary. The target string may not be null-terminated if the length of s2 is n or more. The function returns s1. Write your own version of this function. Test the function in a complete program that uses a loop to provide input values for feeding to the function.
in C programming language char character [100] = "hello"; a string array variable It is given....
in C programming language char character [100] = "hello"; a string array variable It is given. By writing a function called TranslateString, By accessing the pointer address of this given string, returning the string's address (pointer address) by reversing the string Write the function and use it on the main function. Function void will not be written as. Return value pointer address it will be. Sweat operation on the same variable (character) It will be made. Declaration of the function...
. Consider the character array as follows: char mySentence[] = "Hello World!"; The number of characters...
. Consider the character array as follows: char mySentence[] = "Hello World!"; The number of characters in the array can be determined by application of the strlen() function. Therefore, the end of the array can be determined by pointer arithmetic as follows: char * endArray = mySentence + strlen(mySentence); Now that the end of the array has been determined, utilise pointers in C++ to output each character in array in reverse and display it to the console window. What other...
Write a C++ function that lets the user enter alphabet letters into a static char array...
Write a C++ function that lets the user enter alphabet letters into a static char array until either the user enters a non-alphabet letter or, it has reached the MAXSIZE. You can use the isalpha([Char]) function to check if the input is an alphabet letter or not. void fillArray (char ar[], size_t& size){ // this is the function prototype }
for C program 10 by 10 char array. char 0-9 as rows and char a-j as...
for C program 10 by 10 char array. char 0-9 as rows and char a-j as collumns.
[ Write in C, not C++] Define a function char* deleteSymbol(char *s, char x) that removes...
[ Write in C, not C++] Define a function char* deleteSymbol(char *s, char x) that removes the character x from string s. For s[] = “America”, a call to deleteSymbol(s, ‘a’) converts s[] = “Ame”
Create a C module convertTo_csv.c that will implement the function loadAndConvert(const char* file) The code must...
Create a C module convertTo_csv.c that will implement the function loadAndConvert(const char* file) The code must be split into 2 files (.h file and a .c file). The convertTo_csv.c will have the function implementation in ti and the The convertTo_csv.h file will have the declaration. One argument will be given to the function, that is the name of the input file that needs to be converted. A function will create a new csv file called output.csv Know that the loadAndConvert...
In this task you will implement a C++ function with arguments: a sorted integer array, size...
In this task you will implement a C++ function with arguments: a sorted integer array, size of the array, and a target integer value. Find all combinations of two elements in the sorted array which sum up to the target value. When found, add the combinations into an array, and print it. Where there is greater than one combination, you may use the number 200 as a separator for the combinations in the output array. Do not use more than...
Implement stack in C Struct: struct patients{ int id; int severity; char *firstName; char *lastName; char...
Implement stack in C Struct: struct patients{ int id; int severity; char *firstName; char *lastName; char *state; int time_spent; }; Given Array: struct patients* patientsArray[4] = {&p1, &p2, &p3, &p4}; Create two functions one for pushing this array to the stack and one for popping the variables such as p1 p2 p3 p4 by its ID
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT