C Programming

Strcpy() in C

As you know, C is the structured programming language. The C language performs many functions, and strcpy() is one of those functions. The strcpy() function is used to copy the character array from the origin and place the character array at the target point. The strcpy() function only accepts two arguments: a string and a character array.

Syntax:

Let’s look at the predefined writing style of the strcpy() function, where we copy the character string from the origin to the target point.

In this syntax, first, we have to use the function strcpy(), which will show which function we want to perform. Then we have taken two parameters, “origin” and “target”. The “target” parameter is where we want to copy the string and the “origin” is the original string.

As you see above, we have used “constant” with the origin parameter, which will show that the strcpy() function is not allowed to change the character string. This function will not compare the length of two strings, so this means that the length of the targeted string is greater than or equivalent to the original string.

The strcpy() function only works when the string library in the C language is available. To use this function, we include the “string.h” header file in the program.

Example 01:

Here is a simple example of the strcpy() function, where we copy the exact same character string from one place to another place by using the strcpy() function. To run the program, you must have to use the header files with the preprocessor directive “#include”, as you have seen in the “stdio.h header file that came with every translator is included.

We have also included a “string.h” header file to execute our program with the extension “.h” because with the header file “string.h” we can run the strcpy() function. Without this function, we can’t run the program.

Then we started our main() function. Every C program does have a main() function that is required to be called main. The main() function is where the program is executed from. Redirecting requests to certain other program methods often manages how the program is executed in the main() function.

In the main() method, first, we have declared a character type array “origin” of length 30 containing the string message “Welcome To C Programming World”. Then we declare another character type array “target” of length 50. As you have noticed, we have used the “const” modifier at the start of the declaration of the “origin” array. This means the value of the “origin” array can’t be altered throughout the program execution.

As you see, we have set the length of the character array “origin” to 30 and the length of the character array “target” to 50 because this function will not compare the lengths of both arrays. That’s why we have given the maximum length to the “target” array.

#include <stdio.h>

#include <string.h>

int main()

{

  const char origin[30] = "Welcome To C programming World";
  char target[50];
 
  strcpy(target, origin);
  printf("After Copying....\n");
  printf("The Final String is : %s\n",target);

  return 0;


}

After declaring the variables, we used the strcpy() function to copy the origin string and place it at the target point. And then we have to print to the character array “target” by using the print() method to display the string on the output screen. As you see, we have used the “\n” specifier that is used to add a new line in the executable code. And then we return 0 to the main() function, which will show the termination of the main() function or the completion of the program.

Here is the output of the simple illustration that will show the final string after copying the character string from the origin to the target.

Example 02:

Let’s move on to the next example of the strcpy(). In this example, we will copy the string1 message to different parameters. To start our program execution, we have included 3 different header libraries. Each of the header files has different functionality. First, we must include the “stdio” header file with the “.h” extension and preprocessor directive “#include” at the start of the header file. The header file “#include<stdio.h>” is used to get the input and display the output throughout the program execution.

Then we have to include the second important header library “string” with the “.h” extension. The header file “string.h” is used when we call the strcpy() function. Then we have another header file that is “stdlib” with the ”.h” extension at the end of the header file. The header file “stdlib.h” stands for the standard library that contains methods for data storage, control activities, calculations, and other things.

After including header files in the program, we have to start the actual implementation of our program logic in the main() function. Now, we have declared a character type array named “string1” without assigning the array length in the main() function. The character array “string1” contains a string message “Hello World”. Then we have called a print() method that will display the string message that is stored in “string1”. We have declared our second variable “string2” of the type character without declaring the length of the array. In this array, we have stored the “Welcome to C Programming World” message string. Then again, we have called the print() method to display the message that is stored in the “string2” as you see below:

#include<stdio.h>

#include<string.h>

#include<stdlib.h>

int main ()

{

    char string1[]="Hello World";
    printf("String1: %s\n",string1);
    char string2[] = "Welcome To C Programming World";
    printf("String2: %s\n",string2);
   
    char string3[100];
    char string4[100];
   
    printf("The content of the string1 will be copied to string2..\n");
    printf("\nString2: ");
    strcpy(string2, string1);
    printf("%s\n",string2);
   
    strcpy(string3, "String3: ");
    printf("The content of the string2 will be copied to string3..\n\n");
    printf("%s",string3);
    strcpy(string3,string2);
    printf("%s\n",string3);

In lines 14 and 15, we have declared a character type array “string3” and “string4” of the same length of 100. Now we have not assigned any string value to those arrays. After that, we have to use a printf() function that will display a message saying “The content of string1 will be copied to “string2”.  Then we used the strcpy() function to copy the content from “string1” to “string2”. In other words, the value of string2 was replaced by the value of string1, which is “Hello World.” As it is, we replaced the value of string3 and string4 with string1.

printf("The content of the string3 will be copied to string4..\n");

    printf("\nString4: ");
    strcpy(string4,string3);
    printf("%s\n", string4);

    return 0;

}

Now you see the output of the above-compiled program. That will show us that first, we have two strings that contain string messages. String1 contains “Hello World” and string2 contains “Welcome to C Programming World”. After implementing the strcpy() function, the value of string2 was replaced with the values of String1 and so on. At the end, we have four strings. All four strings contain the “Hello World” string message.

Conclusion:

In this article, we have learned strcpy() function. We have discussed why we use the strcpy() function and how the strcpy() function works. We practically work on the strcpy() function by implementing multiple examples, and we also explain these examples in detail so that no point of confusion will be left.

About the author

Kalsoom Bibi

Hello, I am a freelance writer and usually write for Linux and other technology related content