I have a method that concatenates two c string. The method returns a new one. It works fine. Is there a way to make this method void, and just modify the first one?
EDIT: I cannot use strcopy or any other function from the string.h library. It is also suppose to be up to the caller to make sure s1 has enough space to accommodate s2.
char *str_glue(char *s1, char *s2) { char *r; //return string int len1, len2; int i, j; len1 = str_length(s1); len2 = str_length(s2); if ((r=(char*)malloc(len1 + len2 + 1))==NULL) { return NULL; } for (i=0, j=0; i<len1; i++, j++) { r[j] = s1[i]; } for (i=0; i<len2; i++, j++) { r[j] = s2[i]; } r[j] = '\0'; return r; } int main() { char* x = "lightning"; char* y = "bug"; char *z = str_glue(x, y); printf("%s\n", z); }
strcat()does what you want.