How to implement the function strcpy() from string.h to copy a string to other string in C

1 Answer

0 votes
#include <stdio.h>
  
#define LEN 30

char *my_strcpy(char to[], char from[]);

int main(int argc, char **argv) 
{ 
    char s1[LEN] = "", s2[LEN] = "programming is fun", s3[LEN], s4[LEN];
   
    my_strcpy(s1, s2);
    printf("s1 = %s s2 = %s\n", s1, s2); // s1 = "programming is fun" s2 = "programming is fun"
    my_strcpy(s3, "I love to code in C");
    printf("s3 = %s\n", s3); // s3 = "I love to code in C"
    printf("s4 = %s", my_strcpy(s4, "string for s4")); 
    
    return 0;
}

char *my_strcpy(char to[], char from[])
{
    int i = 0;
    
    while ( (to[i] = from[i]) != '\0')
        i++;
        
    return to;
}

/*
  
run:
   
s1 = programming is fun s2 = programming is fun
s3 = I love to code in C
s4 = string for s4

*/

 



answered Oct 23, 2015 by avibootz

Related questions

...