C - strcpy() function
C strcpy() function - copy a string and return a pointer to the end of the result
The strcpy() function is used to copy string2, including the ending null character, to the location that is specified by string1.
Why and when to use strcpy()
- String Copying: strcpy() is used to copy one string into another.
- Overwriting Content: It can replace the contents of a destination string with the source string.
- Initial String Setup: Often used to initialize strings with specific content.
- Avoiding Modifications to Original String: Helps maintain a copy of the original string for comparison or manipulation without altering it.
Syntax:
char *strcpy(char *string1, const char *string2)
Parameters:
Name | Description | Required /Optional |
---|---|---|
string1 | Destination string. | Required |
string2 | Null-terminated source string. | Required |
Return value from strcpy()
Returns the destination string. No return value is reserved to indicate an error.
Examples: strcpy() function
Example 1: How strcpy() copies the contents of string2 to string1
This example demonstrates how strcpy() copies the contents of string2 to string1. Initially, both string1 and string2 contain "String1" and "String2" respectively. After strcpy(string1, string2), string1 now matches string2, both containing "String2". This illustrates how strcpy() overwrites the contents of the destination string.
Code:
#include <stdio.h>
#include <string.h>
#define SIZE 40
int main(void)
{
char string1[SIZE] = "String1";
char string2[SIZE] = "String2";
char * return_string;
printf("Before applying strcpy() function:");
printf("\nstring1 = %s",string1);
printf("\nstring2 = %s",string2);
printf("\n\nAfter applying strcpy() function -> strcpy(string1, string2):");
strcpy(string1, string2);
printf("\nstring1 = %s",string1);
printf("\nstring2 = %s",string2);
}
Output:
Before applying strcpy() function: string1 = String1 string2 = String2 After applying strcpy() function -> strcpy(string1, string2): string1 = String2 string2 = String2
Example 2: Copying a String to Initialize a Greeting
Code:
#include <stdio.h>
#include <string.h>
int main() {
// Define the greeting template
char greeting[50] = "Hello, ";
// Define a name to add to the greeting
char name[] = "Gamil";
// Copy the name into the greeting after "Hello, "
strcpy(greeting + 7, name);
// Print the complete greeting
printf("Full Greeting: %s\n", greeting);
return 0;
}
Output:
Full Greeting: Hello, Gamil
Explanation:
- Initializes the greeting with "Hello, ".
- Copies name into greeting at position 7, adding it after "Hello, ".
- Outputs the complete greeting, resulting in "Hello, Gamil ".
Example 3: Using strcpy() to swap string values using a temporary buffer
Code:
#include <stdio.h>
#include <string.h>
int main() {
// Define two strings
char str1[20] = "Apple";
char str2[20] = "Orange";
char temp[20]; // Temporary storage
// Print original values
printf("Before swap:\nstr1 = %s\nstr2 = %s\n", str1, str2);
// Copy str1 to temp
strcpy(temp, str1);
// Copy str2 to str1
strcpy(str1, str2);
// Copy temp (original str1) to str2
strcpy(str2, temp);
// Print swapped values
printf("After swap:\nstr1 = %s\nstr2 = %s\n", str1, str2);
return 0;
}
Output:
Before swap: str1 = Apple str2 = Orange After swap: str1 = Orange str2 = Apple
Explanation:
- Defines str1 and str2, containing "Apple" and "Orange".
- Copies str1 into temp, then str2 into str1.
- Copies temp into str2, completing the swap.
- Displays the swapped results.
C Programming Code Editor:
Previous C Programming: C strcoll()
Next C Programming:C strncpy()
It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.
https://www.w3resource.com/c-programming/string/c-strcpy.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics