The strcpy() enables you to transfer the contents of a single string variable to another. It provides a convenient way to easily duplicate the content of a string and store it in a different string variable. You must pass in the destination string and the source string as arguments so that the function then uses these arguments to copy the characters from the source string to the destination string, making a replica. When you need to edit or manipulate strings in your C applications, this function is useful.
Syntax
The syntax to use the strcpy() function in C is defined as follows:
Here, the source string that has to be copied to the destination string is indicated by the character pointer *source, and the destination string is indicated by the character pointer *destination.
The strcpy() function is included in the <string.h> header file, which is required to be included at the beginning of the program. The usage of the function typically involves creating two-character arrays, one to hold the original string to be copied and another to store the copied string.
How strcpy() Function Works?
When we use strcpy() function, it first checks the destination string if it has enough size to store the source string. If the space is enough, then it copies the source string to the destination string, and if it doesn’t have enough space, then it causes a buffer overflow, leading to undefined behavior and unexpected results. Moreover, the function returns the destination string’s base address, making it possible to be used in a larger expression, such as concatenating more strings.
It is worth mentioning that the strcpy() function doesn’t cater to overlapping strings. If the destination string and source string overlap, the results are undefined.
How to Use strcpy() Function in C?
Now, let us look at a simple example of a program that demonstrates the use of the strcpy() function:
int main()
{
char originalStr[100], copyStr[100];
printf("Enter a string to copy: ");
fgets(originalStr, sizeof(originalStr), stdin);
strcpy(copyStr, originalStr);
printf("\nSource string: %s\n", originalStr);
printf("Copied string: %s\n", copyStr);
return 0;
}
In this program, we first declare two-character arrays: the original string and the copy string. The user is prompted to enter a string to copy using the fgets() function, which reads input from the standard input (stdin). The original string is then copied to the copied string using the strcpy() function. Finally, the program prints the original and copied strings.
Output
Moreover, since the fgets() function considers the newline character as part of the input, to remove it, we can use the string function strlen() to determine the size and modify the last character to be a null (‘\0’) character.
The newline character has been removed in this updated version of the same code.
int main()
{
char originalStr[100], copyStr[100];
printf("Enter a string to copy: ");
fgets(originalStr, sizeof(originalStr), stdin);
originalStr[strlen(originalStr) - 1] = '\0';
strcpy(copyStr, originalStr);
printf("\nSource string: %s\n", originalStr);
printf("Copied string: %s\n", copyStr);
return 0;
}
The strcpy function in C is used in the above program to copy a string. The two-character arrays originalStr and copyStr are first declared. The fgets method asks the user to provide a string to copy. The input string’s newline character is removed using strlen and the null terminator. Next, strcpy is used to copy the original string to the copied string. Finally, printf is used to display the original and copied strings on the console.
Output
Conclusion
The strcpy() function in C is widely used to copy one string to another. It provides an easy way to duplicate string content and store it in a different variable. Bypassing the destination and source strings as arguments, strcpy() creates a replica by copying characters. However, care should be taken to avoid buffer overflow and undefined behavior.