COMPUTER SCIENCE, i have a simple C function that i would like to help to how to
ID: 3666470 • Letter: C
Question
COMPUTER SCIENCE, i have a simple C function that i would like to help to how to write it out.
void kstrcat(kstring *destp, kstring src)
Concatenates str onto the end of *destp. First extends *destp to be long enough to hold the contents of both strings, by calling kstrextend(). Then copies the contents of src into the dest
So, for example, if we execute the following code:
Now a should have length 12, and the contents "helloworld".
Note that this function takes a pointer to the destination kstring. That means that changes you make to the destp->length and destp->data members will be visible to the caller.
Note: Remember to save the original length of *destp before calling kstrextend(). Otherwise, you won't know where to copy src's data to!
Explanation / Answer
void kstrcat(kstring *destp, kstring src){
int lenDest= strlen(destp);
int lenSrc = strlen(src);
destp=(char*)realloc(destp,lenDest*sizeof(char));
int i = 0;
while(src[i] !=''){
destp[i+lenDest+1] = src[i];
}
destp[i+lenDest+1] = '';
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.