Library Function strcpy in String Class

Library Function strcpy of string.h Header File in C

On this page we will discuss about library function strcpy in string class which is used in C.
The C header file string.h contains a set of functions that can be used for performing various manipulation operations on strings. The strcpy function copies a string from one location(source) to another(destination).
Library Function strcpy in String Class

Library Function strcpy in String Class used in C

In C programming language the strcpy function is included in string.h header file.

This function copies the string pointed by source into the memory pointed by destination and returns a pointer to the resulting string.

Declaration of strcpy function

 char *strcpy(char *dest, const char *src);

Parameters of strcpy function

The strcpy function takes two parameters:

ParameterDescription
sourceIt is a pointer to a source character array (src).
destinationIt is a pointer to a destination character array (dest).

Return value of strcpy function

The strcpy function returns a pointer to the destination character array. This allows you to use the strcpy function as the target of an assignment.

Implementation of Library Function string.h strcpy

Example 1:

Run
#include <stdio.h>
#include <string.h>

int main()
{
    // Initializing the two strings
    char src[30];
    char dest[30];

    printf("Enter the first string (src): ");
    gets(src);

    printf("Enter the second string(dest): ");
    gets(dest);

    printf("\nCopying first string into second... \n\n");
    // copy the contents of src to dest
    strcpy(dest, src); 

    printf("First string (src) = %s\n", src);
    printf("Second string (dest) = %s\n", dest);

    printf("\nCopying \"PrepInsta\" string into src ... \n\n");
    // copy PrepInsta to src
    strcpy(src, "PrepInsta"); 

    printf("\nCopying \"Prime\" string into dest ... \n\n");
    // copy Prime to dest
    strcpy(dest, "Prime"); 

    // Displaying the strings 
    printf("The first string (src) = %s\n", src); 
    printf("The second string (dest) = %s\n", dest);

    return 0;
}

Output:

Enter the first string (src): Hello
Enter the second string(dest): World

Copying first string into second... 

First string (src) = Hello
Second string (dest) = Hello

Copying "PrepInsta" string into src ... 

Copying "Prime" string into dest ... 

The first string (src) = PrepInsta
The second string (dest) = Prime

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription