How to implement the strcpy function without using the c++ <string> library?

Asked

Viewed 215 times

-2

I was unable to develop an effective code, since I do not understand the language as I should. It doesn’t leave that part : char *strcopy(char *destination, char *origin)

  • 1

    Have you tried anything? Have you read the function documentation? Have you been able to find its documentation?

  • I couldn’t find.

1 answer

0


The documentation says that the strcpy function does the following:

Copies the Character string pointed to by src, including the null Terminator, to the Character array Whose first element is pointed to by dest.

So, with a simple while, I copy the source to the target until I copy null Terminator ' 0'.

char* strcpy2(char* dest, const char* source)
{
    int i = 0;

    while ((dest[i] = source[i]) != '\0')
    {
        i++;
    }

    return dest;
}
  • 1

    What is the purpose of this code? Could you explain its relevance with the context of the question?

  • 1

    If I understand correctly, he wants to implement the strcpy function "at hand" and is not succeeding. Above is a possible implementation.

  • 1

    It’s from a list I’m responding to for study. That’s exactly it, Arthur, thank you so much!

  • @Arthurpassos, could you explain how you arrived at this solution? So that those who arrive here can understand the resolution, and not just replicate code?

  • @Jeffersonquesado I did my best. It’s an extremely simple code, it’s hard to explain.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.