strcpy in C Language – String Library Function
Introduction:
We have looked at the introduction to strings in C and strlen library function in earlier articles, In today’s article, We will look at the strcpy in c programming language.
📢 This Program is part of the C String Practice Programs series
The strcpy in C Language:
The strcppy() function is used to copy a string from the source to the destination location. The strcpy() function is available as part of the string.h header files, So we need to include the string.h header file to access the strcpy() function.
We can also copy an original string to a new string by looping through the elements of the original string and copying character by character to the new string (using a Loop). but it is recommended to use the strcpy() library function.
Let’s look at the syntax of the strcpy function.
strcpy in c Syntax:
Here is the syntax of the strcpy() function
1 |
char * strcpy(char * dest, char * src); |
The strcpy() function copies the string pointed by the src to the dest string. The strcpy() function also copies the terminating NULL character( \0) from the original string.
We can use the strcpy function like below to copy a sourceString to destinationString – strcpy(destinationString, sourceString);
The strcpy() function doesn’t check the bounds, So the destination string should be large enough to store the original string, Otherwise strcpy() behavior is undefined.
Parameters:
The strcpy() function takes two strings as input. The first argument is the destination string ( dest) and the second argument is the Source string( src). The strcpy() function copies the source string to the destination string.
Return Type:
The strcpy() function returns the pointer to the destination string ( dest).
Let’s look at a program to copy a string using the strcpy() function.
Copy a string using the strcpy in C Programming Language:
This program copies the string pointed by the src to the dest string.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
/*     program to understand the strcpy function in c     sillycodes.com */  #include<stdio.h> #include<string.h>    // for 'strcpy' function  const int MAXSIZE = 100;  int main() {      // declare two string with 'MAXSIZE'     char src[MAXSIZE], dest[MAXSIZE];      // Read the string from the user     printf("Enter source string : ");     scanf("%s", src);      // copy the 'src' to 'dest'     strcpy(dest, src);      printf("Original String: %s\n", src);     printf("New String: %s\n", dest);      return 0; } |
We need to include the string.h header file to access the strcpy() function.
In the above program, We declared two strings called src, dest with the size of MAXSIZE. The MAXSIZE is a global constant with the 100 value. So the strings src and dest can store at max 100 characters.
We prompted the user to provide the source string and read it using the scanf function. Then we used the strcpy() function to copy the input string src to the destination string dst.
Once the above strcpy() operation is completed, the dest string also contains the src string.
Finally, We printed the original string ( src) and Destination string ( dest) on the console.
Program Output:
Compile and run the program
1 2 3 4 5 6 7 8 9 10 |
$ gcc strcpy-func.c $ ./a.out Enter source string : programming Original String: programming New String: programming $ ./a.out Enter source string : Tennis Original String: Tennis New String: Tennis $ |
As you can see from the above output, The strcpy function is properly copying the source string into the destination string.
📢 The destination string must have enough size to store the source string
Example 2: strcpy to copy string with spaces and special characters:
We can also copy the string with the spaces and special characters using the strcpy() function. Here is the program to demonstrate the same.
Copy String with Spaces using strcpy function:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
/*     program to understand the strcpy function in c string with spaces     sillycodes.com */  #include<stdio.h> #include<string.h>    // for 'strcpy' function  #define MAXSIZE 100  int main() {      // Initialize the 'src'     char src[MAXSIZE] = "Programming is Fun";     char dest[MAXSIZE];      // copy the 'src' to 'dest'     strcpy(dest, src);      printf("Original String: %s\n", src);     printf("New String: %s\n", dest);      return 0; } |
Program Output:
1 2 3 4 5 |
$ gcc strcpy-space.c $ ./a.out Original String: Programming is Fun New String: Programming is Fun $ |
As we can see from the above output, The strcpy() function properly copies the string with spaces.
The strcpy function to copy string with special characters:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
/*     program to understand the strcpy function in c using special characters     sillycodes.com */  #include<stdio.h> #include<string.h>    // for 'strcpy' function  #define MAXSIZE 100  int main() {      // Initialize the 'source'     char source[MAXSIZE] = "A&#@&#";     char destination[MAXSIZE];      // copy the 'source' to 'destination'     strcpy(destination, source);      printf("Original String: %s\n", source);     printf("New String: %s\n", destination);      return 0; } |
Program Output:
Compile and Run the program
1 2 3 4 5 |
$ gcc strcpy-special.c $ ./a.out Original String: A&#@&# New String: A&#@&# $ |
The strcpy is able to copy the source string with special characters to a new string(i,e destination string).
Example 3: Nested strcpy() function:
We can use the nested strcpy() function to copy strings. Here is the program to demonstrate the nested strcpy() function in c language.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
/*     program to understand the Nested strcpy function in c     sillycodes.com */  #include<stdio.h> #include<string.h>    // for 'strcpy' function  int main() {     // declare destination     char source[] = "Albert Einstein";     char destination[50];     char finalDestination[100];  // :)      // Create a Nested strcpy() function     strcpy(finalDestination, strcpy(destination, source));      // Print the strings     printf("Source String: %s\n", source);     printf("Destination String: %s\n", destination);     printf("FinalDestination String: %s\n", finalDestination);      return 0; } |
In the above code, We used the strcpy(finalDestination, strcpy(destination, source)); to copy the source to the destination and finalDestination strings.
Program Output:
1 2 3 4 5 6 |
$ gcc strcpy-space.c $ ./a.out Source String: Albert Einstein Destination String: Albert Einstein FinalDestination String: Albert Einstein $ |
If we look at the output, All three strings will contain the string Albert Einstein.
Conclusion:
In this article, We have looked at the strcpy() library function in C language. We looked how to access and use the strcpy to copy a string to new string with different examples.
2 Responses
[…] strcpy library function with Example Programs […]
[…] this method, We are going to use the strcpy library function to copy the strings. Let’s look at the step by step explanation of the […]