Search code examples
arrayscfunctionc-strings

Implementing strcpy function without using in-built strcpy


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


Solution

  • In the olden days when K&R was king, we'd tie a onion to our belts, as was the style at the time and do something like

    void strcopy(char *a, char *b)
    {
       while ( *b++ = *a++ ) {}
    }
    

    Now I think the old ways are depreciated

    It has been suggested that I explain why this works.

    *b = *a does the core function of copying of one byte

    *b++ = *a++ Adding the auto-increments, moves both the pointers to the next locations in memory after the assignment is done

    while (*b++ = *a++) {} loops while the byte copied is non-zero. the empty {} is just to complete the syntax of the while statement

    So the while loop will run copying bytes then incrementing both pointers to the next location until the byte copied was zero, being the end of string