I am trying to implement a strcpy function without using inbuilt function. I have searched online but I only found the **programs** to do this task but not **functions**.

I tried various approaches but no luck. At the end (Try 4) , I wrote a code that works but I dont understand why the hell is that thing working but previous approaches aren't

Just looking for a function that can copy value of one string into another when called from main() and displays correct value in output string. I would be really thankful if I could get that. And ofcourse, I cant use the inbuilt function because I am forced to do this cuz of an exam on Monday where we will have to implement any of the random 7 string functions and explain the code too..

EDIT: Also found this <https://www.programmersought.com/article/29163684184/> code to implement the same thing but it is written in C++. Why does schools/colleges still teach C after the invention of C++? Anyone knows any C++ to C converters?

Try 1:

```
#include <stdio.h>
void strcopy(char *a, char *b)
{
    for (int i = 0; a[i] != '\0'; i++)
    {
        b[i] = a[i];
    }
}
int main()
{
    char a[10];
    printf("Enter string: ");
    scanf("%s", &a);
    printf("Input string: %s", a);
    char b[(sizeof(a))];
    strcopy(a,b);
    printf("\nOutput string: %s", b);

    return 0;
}

```

Output:

```

Enter string: helloworld
Input string: helloworld
Output string: helloworldhelloworld

```

---

Try 2:

```
#include <stdio.h>
void strcopy(char *a, char *b)
{
    for (int i = 0; a[i] != '\0'; i++)
    {
        b[i] = a[i];
    }
}
int main()
{
    int n;
    printf("Enter the string size: ");
    scanf("%d",&n);
    char a[n];
    printf("Enter string: ");
    scanf(" %s",&a);
    printf("Input string: %s", a);
    char b[n]; 
    strcopy(a,b);
    printf("\nOutput string: %s", b);

    return 0;
}

```

Ouput:

```

Enter the string size: 7
Enter string: helloworld
Input string: helloworld  
Output string: helloworldö

```

---

Try 3:

```
#include <stdio.h>
void strcopy(char *a, char *b)
{
    for (int i = 0; a[i] != '\0'; i++)
    {
        b[i] = a[i];
    }
}
int main()
{
    int n;
    printf("Enter the string size: ");
    scanf("%d",&n);
    n++;
    char a[n];
    printf("Enter string: ");
    // scanf(" %s",&a);
    fgets(a,n,stdin);
    fgets(a,n,stdin);  //need to write twice or it wont work
    printf("Input string: %s", a);
    char b[n]; 
    strcopy(a,b);
    printf("\nOutput string: %s", b);

    return 0;
}

```

Output:

```

Enter the string size: 5
Enter string: helloworld
Input string: hello  
Output string: hello⌂

```

---

Try 4:

```
#include <stdio.h>
void strcopy(char *a, char *b)
{
    for (int i = 0; a[i] != '\0'; i++)
    {
        b[i] = a[i];
    }
}
int main()
{
    int n;
    printf("Enter the string: ");
    scanf("%d",&n);
    n++;
    char a[n];
    scanf(" %s",&a);
    // printf("Enter string: ");
    // fgets(a,n,stdin);
    // fgets(a,n,stdin);
    printf("Input string: %s", a);
    char b[n]; 
    strcopy(a,b);
    printf("\nOutput string: %s", b);

    return 0;
}

```

Output:

```

Enter the string: helloworld
Input string: helloworld 
Output string: helloworld

```

---

**EDIT:**

Trying approach given by a friend in answers:

Code:

```
#include <stdio.h>
void strcopy(char *a, char *b)
{
    for (int i = 0; a[i+1] != '\0'; i++)
    {
        b[i] = a[i];
    }
}
int main()
{
    // int n;
    printf("Enter the string: ");
    // scanf("%d",&n);
    // n++;
    char a[10];
    scanf("%s",&a);
    // printf("Enter string: ");
    // fgets(a,n,stdin);
    // fgets(a,n,stdin);
    printf("Input string: %s", a);
    char b[sizeof(a)]; 
    strcopy(a,b);
    printf("\nOutput string: %s", b);

    return 0;
}

```

Output:

```

Enter the string: helloworld
Input string: helloworld
Output string: helloworl

```

---

**EDIT 2:**

Trying do-while loop:

Code:

```
#include <stdio.h>
void strcopy(char *a, char *b)
{
    // for (int i = 0; a[i+1] != '\0'; i++)
    // {
    //     b[i] = a[i];
    // }
    int i=0;
    do{
        b[i]=a[i];
        i++;
    }while(a[i] != '\0');
}
int main()
{
    // int n;
    printf("Enter the string: ");
    // scanf("%d",&n);
    // n++;
    char a[10];
    scanf("%s",&a);
    // printf("Enter string: ");
    // fgets(a,n,stdin);
    // fgets(a,n,stdin);
    printf("Input string: %s", a);
    char b[sizeof(a)]; 
    strcopy(a,b);
    printf("\nOutput string: %s", b);

    return 0;
}

```

Output:

Enter the string: helloworld
Input string: helloworld
Output string: helloworldhelloworld