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

Write a function int strlen(char s1[]) which returns the length of the char array s1.
Write a function int strlen(char s1[]) which returns the length of the char array s1.
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.
Add to the class DoublyLinkedList a method called isSubSet ( DoublyLinkedList <Character> s1, DoublyLinkedList <Character> s2)...
Add to the class DoublyLinkedList a method called isSubSet ( DoublyLinkedList <Character> s1, DoublyLinkedList <Character> s2) that receives 2 DoublLinked lists of characters. The method will return true if s1 is subset of s2. Then don’t forget to test the method in the main method in Test class in Doubly package.
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 }
(C LANGUAGE) Write a function called upperLower that inputs a line of text into char array...
(C LANGUAGE) Write a function called upperLower that inputs a line of text into char array s[100]. Output the line in uppercase letters and in lowercase letters This is what I have so far: void * upperLower (const char * s) { char *word[sizeof(s)]; for(int i = 0; i < sizeof(s); i++){ *word[i] = s[i]; } word = strtok(word, " "); int counter = 0; while(word != NULL){ if(counter % 2 == 0){ word[counter] = toupper(word); printf("%s ", word); }...
Please write code in c++ using iostream library. Write a function bool cmpr(char * s1, int...
Please write code in c++ using iostream library. Write a function bool cmpr(char * s1, int SIZE1, char * s2, int SIZE2) that compares two strings. Input Input contains two strings. Each string is on a separate line. example: aqua aqua Output Output YES if given two strings are the same or NO otherwise. YES
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.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT