strcpy in C Language – String Library Function

Spread the love
strcpy-in-c-with-example-programs

Introduction:

We have looked at the introduction to strings in C and strlen library function in earlier articles, In today’s article, We will look at the strcpy in c programming language.

📢 This Program is part of the C String Practice Programs series

The strcpy in C Language:

The strcppy() function is used to copy a string from the source to the destination location. The strcpy() function is available as part of the string.h header files, So we need to include the string.h header file to access the strcpy() function.

We can also copy an original string to a new string by looping through the elements of the original string and copying character by character to the new string (using a Loop). but it is recommended to use the strcpy() library function.

Let’s look at the syntax of the strcpy function.

strcpy in c Syntax:

Here is the syntax of the strcpy() function

The strcpy() function copies the string pointed by the src to the dest string. The strcpy() function also copies the terminating NULL character( \0) from the original string.

We can use the strcpy function like below to copy a sourceString to destinationStringstrcpy(destinationString, sourceString);

The strcpy() function doesn’t check the bounds, So the destination string should be large enough to store the original string, Otherwise strcpy() behavior is undefined.

Parameters:

The strcpy() function takes two strings as input. The first argument is the destination string ( dest) and the second argument is the Source string( src). The strcpy() function copies the source string to the destination string.

Return Type:

The strcpy() function returns the pointer to the destination string ( dest).

Let’s look at a program to copy a string using the strcpy() function.

Copy a string using the strcpy in C Programming Language:

This program copies the string pointed by the src to the dest string.

We need to include the string.h header file to access the strcpy() function.

In the above program, We declared two strings called src, dest with the size of MAXSIZE. The MAXSIZE is a global constant with the 100 value. So the strings src and dest can store at max 100 characters.

We prompted the user to provide the source string and read it using the scanf function. Then we used the strcpy() function to copy the input string src to the destination string dst.

Once the above strcpy() operation is completed, the dest string also contains the src string.

Finally, We printed the original string ( src) and Destination string ( dest) on the console.

Program Output:

Compile and run the program

strcpy-function-in-c-program-output

As you can see from the above output, The strcpy function is properly copying the source string into the destination string.

📢 The destination string must have enough size to store the source string

Example 2: strcpy to copy string with spaces and special characters:

We can also copy the string with the spaces and special characters using the strcpy() function. Here is the program to demonstrate the same.

Copy String with Spaces using strcpy function:

Program Output:

strcpy-function-in-c-with-spaces

As we can see from the above output, The strcpy() function properly copies the string with spaces.

The strcpy function to copy string with special characters:

Program Output:

Compile and Run the program

The strcpy is able to copy the source string with special characters to a new string(i,e destination string).

Example 3: Nested strcpy() function:

We can use the nested strcpy() function to copy strings. Here is the program to demonstrate the nested strcpy() function in c language.

In the above code, We used the strcpy(finalDestination, strcpy(destination, source)); to copy the source to the destination and finalDestination strings.

Program Output:

If we look at the output, All three strings will contain the string Albert Einstein.

Conclusion:

In this article, We have looked at the strcpy() library function in C language. We looked how to access and use the strcpy to copy a string to new string with different examples.

Related Tutorials:

Venkat

Hi Guys, I am Venkatesh. I am a programmer and an Open Source enthusiast. I write about programming and technology on this blog.

You may also like...

2 Responses

  1. […] strcpy library function with Example Programs […]

  2. […] this method, We are going to use the strcpy library function to copy the strings. Let’s look at the step by step explanation of the […]

Leave a Reply