I need help to concatenate strings using recursion in C.
I have 2 strings in input, src and dest, and I need to concatenate recursively src to dest, and store the concatenated string in dest.
e.g. if src="house"
and dest="clock"
, the output should be "chlooucske"
.
EDIT: This is my code:
char* str_concatenate(char dest[], char src[], int index){
char temp[256]; // temporaty variable
temp[index]=src[index]; //should copy each letter from src to temp
temp[index+1]=dest[index]; //should copy each letter from dest to temp
dest[index]=temp[index]; //should store the concatenated string into dest
if (src[index]=='\0'){ //base case
return dest;
}
else
return str_concatenate(dest,src,index+1);
}
int main(){ //test
char dest[]="house";
char src[]="clock";
char* ris=str_concatenate(dest,src,0);
printf("dest= %s\n", ris); //should print "chlooucske"
return 0;
}
However it copies the entire word from src to dest and prints it, it does not concatenate letters.