Nathan Anderson
Nathan Anderson

Reputation: 21

How to create my own strcpy function?

I am trying to design a program in which I will create a 3 functions that resemble functions in the c-standard library (strlen,strcmp,strcpy). The first two I have gotten close to finishing, only the last one is the main problem. I am trying to create a function that will have the same functionality as the standard function strcpy. Here is what I have so far.

void myStrncpy(char destination[], const char source[], int count) {

    for (int i = 0 ; source[i] != '\0' ; i++) {

    count++;
    }
}

So far I have gotten the length of "source" and stored it in "count". What is the next step I need to take? I would prefer to use another for loop and if statements if possible. Thanks!

****EDIT****

This is what I have now...

void myStrncpy(char destination[], const char source[], int count) {

    for (int i = 0 ; source[i] != '\0' && destination[i] != '\0' ; i++) {
    destination[i] = source[i];
    count++;
    }

}

OUTPUT:

str1 before: stringone
str2 before: stringtwo

str1 after : stringtwo
str2 after : string two

2ND RUN (WHERE I'M GETTING MY PROBLEM):

str1 before: stringthree
str2 before: stringfour

str1 after: stringfoure
str2 after: stringfour

What else do I need to put in my code so that it copies every letter until it runs out of room, or it copies every letter until it runs out of letters to copy?

Upvotes: 2

Views: 22334

Answers (4)

Rajvardhan Desai
Rajvardhan Desai

Reputation: 11

 //'C' Program to create own version of 'strcpy'  
  
    #include <stdio.h>
    #include <string.h> // for strlen
        
    void my_strcpy(char *str1, char *str2)
    {
        int n = strlen(str2);
        for (int i = 0; i <= n; i++)
        {
            str1[i] = str2[i];
    
            // str1[0] = str2[0]; // n = p
            // str1[1] = str2[1]; // o = r
            // str1[2] = str2[2]; // o = o
            //   .
            //   .
            //   .
            //  until the length of str2
        }
    }
    
    int main()
    {
        char str1[20] = "noob"; // Destination
        char str2[20] = "Pro";  // source
    
        printf("str1 = %s", str1); // Before
        printf("\nstr2 = %s",str2);

        /* calling a function */
        my_strcpy(str1, str2); 
        /* This function will replace str1 with str2 */
    
        printf("\nstr1 = %s", str1); // After
        printf("\nstr2 = %s",str2);
    
        return 0;
    }

  • Output :

          str1 = noob
          str2 = Pro 
    
          str1 = Pro 
          str2 = Pro 
    

  • Without any Comments
#include <stdio.h>
#include <string.h> 

void my_strcpy(char *str1, char *str2)
{
    int n = strlen(str2);
    for (int i = 0; i <= n; i++)
    {
        str1[i] = str2[i];
    }
}

int main()
{
    char str1[20] = "noob"; 
    char str2[20] = "Pro";  

    printf("str1 = %s", str1); 
    printf("\nstr2 = %s",str2);

    my_strcpy(str1, str2);

    printf("\nstr1 = %s", str1); 
    printf("\nstr2 = %s",str2);

    return 0;
}
      

     

Upvotes: 1

/* body of the function*/
/*Mahmoud Mohamed Younis*/
int i;
int j = 0;
for(i=0;i<10;i++)
{
    if(s1[i] == '\0')
    {
      int  index = i;
        while(s2[j]!='\0')
        {
            if(index>10)
               break;
            s1[index] = s2[j];
            j++;
            index++;
        }
        s1[index] = '\0';
        break;
    }
}

Upvotes: 0

Azeemali Hashmani
Azeemali Hashmani

Reputation: 155

/* Include header files
 */

 #include <stdio.h>

/* Pre=processor Directives
 */

 #define word_size   20

/* Function prototype
 */

void my_func_strcpy(char *source, char* destination);


 int main()

 {

        char source[word_size]      = "I am source";
        char destination[word_size] = "I am destination"; 

        printf("The source is '%s' \n", source);
        printf("The destination is '%s' \n", destination);

        /* Calling our own made strcpy function
         */
        my_func_strcpy(source, destination);

        printf("The source data now is '%s' \n");
        printf("The destination data now is '%s' \n");
 }

 /* Function to copy data from destination to source
  */  
 void my_func_strcpy(char *source, char* destination)
 {
    char temp[word_size] = {'\0'};
    int  index    = 0;

    /* Copying the destination data to source data
     */
    while (destination[index] != '\0')
    {
        source[index] = destination[index];
        index++;
    }

    /* Making the rest of the characters null ('\0') 
     */
    for (index = 0; index < word_size; index++)
    {
        source[index] = '\0';
    }
 }

Upvotes: 1

Scott Hunter
Scott Hunter

Reputation: 49803

If you've already written a MyStrcpy, you're almost done:

The stpncpy() and strncpy() functions copy at most n characters from s2 into s1. If s2 is less than n characters long, the remainder of s1 is filled with `\0' characters. Otherwise, s1 is not terminated.

So you have a copy of strcpy that will stop after it has copied n characters; if it stopped earlier than that (b/c you got to the end of s2), fill the rest of s1 w/ /0's.

Upvotes: 2

Related Questions