Orionss Orionss - 1 year ago 59
C Question

How to create a copy of strcat?

I have to create a copy of some elements of the standard library in C and I have to create a copy of strcat. So I have to create a function that concatenate two strings in C. I know arrays in C can't change the allocated size. The only fonction i'm allowed to use is copies i made of strlen, strstr, and write() ... My code looks like this :

char *my_strcat(char *dest, char *src)
int dest_size;
int src_size;
int current_pos;
int free_space;
int pos_in_src;

src_size = my_strlen(src);
dest_size = my_strlen(dest);
while (dest[current_pos] != '\0')
current_pos = current_pos + 1;
free_space = dest_size - current_pos;
if (free_space < src_size)
return (0);
while (src[pos_in_src] != '\0')
dest[current_pos] = src[pos_in_src];
pos_in_src = pos_in_src + 1;
current_pos = current_pos + 1;
return (dest);

But I don't know how to declare my dest and src in the main.
I don't know how to create an array with a big size, declare it as a string like dest = "Hello\0" but this array has to still contains more than 6 characters.

Can you help me please ?

Answer Source
char dest[19] = "epite";
char *src = "chor42spotted";

my_strcat(dest, src);

Also, read the man for strcat(3)

the dest string must have enough space for the result.


So your function is behaving incorrectly, you do not need to check that you have enough free space in dest