The strcpy()
function in C is a standard library function that copies a string from one location to another. It is part of the C standard library (string.h
). This function is useful for duplicating strings or copying them into buffers.
Table of Contents
- Introduction
strcpy()
Function Syntax- Understanding
strcpy()
Function - Examples
- Copying a Simple String
- Copying a String into a Struct
- Real-World Use Case
- Conclusion
Introduction
The strcpy()
function copies a null-terminated string from a source to a destination. The destination string must be large enough to hold the copied string, including the null terminator. If the destination string is not large enough, it can lead to buffer overflows and undefined behavior.
strcpy() Function Syntax
The syntax for the strcpy()
function is as follows:
char *strcpy(char *dest, const char *src);
Parameters:
dest
: A pointer to the destination array where the content is to be copied.src
: A pointer to the source null-terminated string to be copied.
Returns:
- The function returns a pointer to the destination string (
dest
).
Understanding strcpy() Function
The strcpy()
function copies the string pointed to by src
, including the null terminator, to the array pointed to by dest
. The destination array must be large enough to receive the copy. The function returns the destination string.
Examples
Copying a Simple String
To demonstrate how to use strcpy()
to copy a simple string, we will write a simple program.
Example
#include <stdio.h>
#include <string.h>
int main() {
char src[] = "Hello, World!";
char dest[50]; // Ensure the destination is large enough
// Copy the string using strcpy
strcpy(dest, src);
// Print the destination string
printf("Destination string: %s\n", dest);
return 0;
}
Output:
Destination string: Hello, World!
Copying a String into a Struct
This example shows how to use strcpy()
to copy a string into a struct.
Example
#include <stdio.h>
#include <string.h>
typedef struct {
int id;
char first_name[20];
char last_name[20];
} Person;
int main() {
Person p;
p.id = 1;
strcpy(p.first_name, "Ramesh");
strcpy(p.last_name, "Fadatare");
// Print the struct
printf("Person ID: %d, First Name: %s, Last Name: %s\n", p.id, p.first_name, p.last_name);
return 0;
}
Output:
Person ID: 1, First Name: Ramesh, Last Name: Fadatare
Real-World Use Case
Duplicating User Input
In real-world applications, the strcpy()
function can be used to duplicate user input strings for further processing or storage.
Example: Duplicating User Input
#include <stdio.h>
#include <string.h>
int main() {
char user_input[100];
char copy[100];
printf("Enter a string: ");
fgets(user_input, sizeof(user_input), stdin);
// Remove the newline character if present
user_input[strcspn(user_input, "\n")] = '\0';
// Copy the user input using strcpy
strcpy(copy, user_input);
// Print the copied string
printf("Copied string: %s\n", copy);
return 0;
}
Output (example user input "Hello, Ramesh!"):
Enter a string: Hello, Ramesh!
Copied string: Hello, Ramesh!
Conclusion
The strcpy()
function is used for copying strings in C. By understanding and using this function correctly, you can efficiently duplicate strings and handle string data in your programs. Always ensure that the destination array is large enough to hold the copied string to prevent buffer overflows and undefined behavior.
Comments
Post a Comment
Leave Comment