My task is like this: I should implement the strcpy function under the following constraints:
- The function should use pointer expression (
*(d+i)
) - I should implement it without using
<string.h>
I'm programming in Visual Studio 2019. I searched some source code in google and run them, but my program has a logical error. The program ends right away, each time. I don't know what I'm doing wrong. Here's my code in Visual Studio 2019 on Windows. Please tell me what's wrong.
#include <stdio.h>
void strcpy(char*, char*);
int main()
{
char* sen1 = "Hello";
char* sen2 = "Friends";
strcpy(sen1, sen2);
printf("The result: %s\n", sen1);
return 0;
}
void strcpy(char* str1, char* str2)
{
int i = 0;
while (*(str2 + i) != '\0')
{
*(str1 + i) = *(str2 + i);
i++;
}
*(str1 + i) = '\0';
}