0

Create a function that concatenates two strings The function should be created based on the following criteria

  1. The putchar keyword should be used
  2. The prototype must be char *_strcat(char *dest, char *src);
  3. The function should add a terminating null value at the end of dest
  4. The function should return a pointer to dest.

The function below finds the end of the src string and appends it to dest string. I tried it out with my main.c file and got World!Hello instead of Hello World!

void concatenate(char* str1, char* str2) {
    int i = 0, j = 0;

    // Find the end of the first string
    while (str1[i] != '\0') {
        i++;
    }

    // Append the second string to the end of the first string
    while (str2[j] != '\0') {
        str1[i] = str2[j];
        i++;
        j++;
    }

    // Add a null terminator to the end of the concatenated string
    str1[i] = '\0';
}

3
  • I tried it out with my main.c file and got World!Hello instead of Hello World! Seems like you flipped str1 and str2 in your main. Commented Dec 25, 2022 at 18:13
  • We need to see your main function that calls concatenate. You may have the args reversed. Or, if you do: char dst[] = "Hello"; concatenate(dst,"World!"); then you have UB (undefined behavior) because dst does not have enough space to contain the combined string. You'd need (e.g.): char dst[30] = "Hello"; concatenate(dst,"World!"); Commented Dec 25, 2022 at 18:16
  • Kindly show the main function. I believe you misplaced the arguments while calling 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 hold str2. 2. You do not check whether one or both of the pointers are pointing to valid memory, or in other words, are not NULL. Commented Dec 25, 2022 at 18:24

1 Answer 1

2

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;
}
Sign up to request clarification or add additional context in comments.

2 Comments

May I know why you used preprocessor directives ifdef and define?
I used int to declare i and j as they will serve as counters for the iteration. As you know, putchar can only display a character at a time. But why do we use size_t instead?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.