String Copy in C Language

We will write a program for string copy in C programming. This can be done using strcpy() function which is defined in <string.h>. The same thing can be done without using this predefined function.

String Copy without using string manipulation library functions

#include<stdio.h>
int main()
{
   char str1[100], str2[100];
   int i;

   printf("Enter first String: ");
   fgets(str1,sizeof(str1),stdin);

   // str2 is empty or having garbage value
   for(i=0;str1[i]!='\0';i++)
   {
      str2[i]=str1[i];
   }
   str2[i]='\0';

   printf("Second string is:\n");
   puts(str2);

   return 0;
}

Output:-

Enter first String: Know Program
Second string is:
Know Program

We take two string str1 and str2 of size 100. For str1 input is taken from the user. To get the input we used fgets() function, it holds the new line character also at the end of the string. Now, using for loop copy is started. The string str2 will be filled with characters of the string str1.

Copy using strcpy()

We can do string copy in C using strcpy() function also. The strcpy() function copies the string pointed by source(including the null character), it is important to note that the destination string should be large enough otherwise it may result in undefined behavior. It required header file <string.h>

Function declaration of strcpy():-

char *strcpy(char *destinationString,
             const char *sourceString)
#include<stdio.h>
#include<string.h>
int main()
{
    char s1[25]= "Computer";
    char s2[25],s3[25], s4[25], s5[25];

    printf("Enter a string: ");
    fgets(s2, sizeof(s2),stdin);

    strcpy(s3, s1);
    strcpy(s4, s2);
    strcpy(s5,"programming");

    puts(s3);
    puts(s4);
    puts(s5);

    return 0;
}

Output:-

Enter a string: Know Program
Computer
Know Program

programming


Copy using strncpy()

The strncpy() is also used to copy the string, but it sets the maximum number of characters that can be moved. Therefore, strncpy() is a safer function.

Many of the problems associated with unequal string-array sizes can be controlled with the string-number copy function, strncpy(). This function contains a parameter that specifies the maximum number of characters that can be moved at a time.

Function declaration:-

char *strncpy(char *destinationString, 
              const char *sourceString, 
              int size);

In this function, size specifies the maximum number of characters that can be moved and the size must be a positive integer number.

If the source string length is equal or greater than size, then only given the size of characters are moved. In this case, the destination variable may not be a valid string because it has not a null character at the end because the length of the source string was equal or greater than size. The strncpy() function does not insert a null character if the string is longer than size.

#include<stdio.h>
#include<string.h>
int main()
{
   char str1[10];
   char str2[10] = "Program";

   strncpy(str1, str2, 3);
   puts(str1);

   return 0;
}

Output:-

Pro

If the length of the source string is less than the size then the entire string is copied and then null characters are inserted into the destination string until exactly size characters have been copied. Thus it is more correct to think of size as the destination characters that must be filled.

#include<stdio.h>
#include<string.h>
int main()
{
   char str1[10];
   char str2[10] = "Know";

   strncpy(str1, str2, 8);
   puts(str1);

   return 0;
}

Output:-

Know

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!

Leave a Comment

Your email address will not be published. Required fields are marked *