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

Need help coding it In C language!! Let\'s explore working with the C string lib

ID: 3838900 • Letter: N

Question

Need help coding it In C language!!

Let's explore working with the C string library. Remember that C style strings are null terminated char arrays. Write a function that accepts two input strings and a pointer to an empty string and the type of operation to be performed. Possible operations include: concatenate (), append () and replace (). The specifications for the three operations are as follows: Concatenate: The operation accepts the two input strings and a pointer to an empty string. It then builds a string that is the concatenation of the two input strings and returns the result via the pointer to the empty string. concat: S3 leftarrow S1 + S2 S1: "Bon Jour" S2: "mon petit frere" S3: "Bon Jour mon petit frere" S1, S2 remain unchanged Append: The operation accepts the two input strings. It then builds a string that appends the second sting to the first and returns the result via the pointer to the first string. append: S1 leftarrow S1 + "Ca va, mon ami" S1: "Bon Jour Ca va, mon ami" S1 is changed, S2 remains unchanged Replace: The operation accepts the two input strings. It then replaces the first string with the second. replace: S1 leftarrow S2 S1: "Bon Jour" S1 is changed to "mon petit frere", S2 remains unchanged

Explanation / Answer


void concatinate(char *S1, char *S2, char *S3) {
   int j = 0;
   for (int i = 0; i < strlen(S1); i++)
       S3[j++] = s1[i]; //first copy S1

   for (int i = 0; i < strlen(S2); i++)
       S3[j++] = s1[i]; //then copy S2

   S3[j] = ''; //then mark end

}

void append (char *S1, char *S2) {
   int len1, i;
   len1 = strlen(S1);
   for (i = 0; i < strlen(S2); i++)
       S1[len1++] = S2[i]; //start coping after end of S1
   S1[len1] = ''; //mark end of strign
}

void replace (char *S1, char *S2) {
   int i;
   for (i = 0; i < strlen(S2); i++)
       S1[i] = S2[i]; //simply copy fro position 0
   S1[i] = ''; // mark end of string
}

I hope this answer helped you!

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