Your assignment has problems:
Create a function that concatenates two strings.
This statement is not precise enough: should it allocate a new string or concatenate the second string at the end of the first string like strcat()?
The function should be created based on the following criteria.
I would rather write: The function should meet the following requirements.
The putchar keyword should be used.
This is a most ridiculous requirement! putchar is not a keyword, but a macro (an a function) defined in <stdio.h>. Using it in a user function is feasible, but tricky, not something newbies should be required to try and certainly not something any programmer should do.
The prototype must be char *_strcat(char *dest, char *src);
This requirement is bogus:
the identifier is a reserved word (it starts with an _). my_strcat or concat_string would be better choices
at least src should be declared const char *src. And it would indicate that the function likely behaves like strcat, or both arguments should be const qualified and the function should allocate memory.
The function should add a terminating null value at the end of dest.
This is implicit if the result is to be used as a C string.
The function should return a pointer to dest.
At last an indication regarding the expected semantics: the same as strcat.
Your implementation is almost correct, except for this:
the name is not as specified
the argument names are not dest and src
you should use size_t for the type of i and j
you must return dest.
Regarding your observations, you did not post the full program, but it is likely you passed the arguments in the wrong order and/or the destination array that contains Hello is not long enough to receive the concatenated string.
Here is a modified version:
#include <stdio.h>
char *my_strcat(char *dest, const char *src) {
size_t i = 0, j = 0;
#ifdef putchar
// just using the putchar identifier for fun
#endif
// Find the end of the first string
while (dest[i] != '\0') {
i++;
}
// Append the second string to the end of the first string
while (src[j] != '\0') {
dest[i] = src[j];
i++;
j++;
}
// Add a null terminator to the end of the concatenated string
dest[i] = '\0';
// return a pointer to dest.
return dest;
}
int main() {
char hello[20] = "Hello ";
char world[] = "World!";
printf("%s\n", my_strcat(hello, world));
return 0;
}
I tried it out with my main.c file and got World!Hello instead of Hello World!Seems like you flippedstr1andstr2in your main.mainfunction that callsconcatenate. You may have the args reversed. Or, if you do:char dst[] = "Hello"; concatenate(dst,"World!");then you have UB (undefined behavior) becausedstdoes not have enough space to contain the combined string. You'd need (e.g.):char dst[30] = "Hello"; concatenate(dst,"World!");concatenate. Other than that, your function doesn't do what it's supposed to do, (at least, according to the assignment) but:1.You do not check whether the first string is large enough to holdstr2.2.You do not check whether one or both of the pointers are pointing to valid memory, or in other words, are notNULL.