HomeLinuxStrcpy() Perform in C Language

Strcpy() Perform in C Language


The C normal library defines numerous capabilities to deal with the strings within the “string.h” header. The capabilities which are outlined there can copy, concatenate, break up, or carry out every other course of on a given string.

The information of the enter arguments and their knowledge kind in addition to the calling strategies of those capabilities is necessary for the programmer since they’re broadly utilized in most applications of the C language and require fluent dealing with.

On this Linuxhint article you’ll learn to use the strcpy() operate to repeat the strings.
We’ll present you the syntax, the enter arguments, and the information sorts that every of them accepts. Then, we’ll apply what we realized in sensible examples that embrace the code snippets and pictures which present the usage of strcpy() in numerous circumstances and with completely different calling strategies.

Syntax of the Strcpy() Perform in C Language

char *strcpy(char *s1, char *s2);

Description of the Strcpy() Perform in C Language

The strcpy() operate copies the string that’s pointed to by “s2” to “s1”. Consequently, it returns a pointer to the string with the brand new copy. The copy is a literal and accommodates management, particular, and escape characters. Like all string processing capabilities, strcpy() inserts a null character after the final character of the brand new string.

The strcpy() operate accepts a pointer, array, or an express string as an enter argument to “s2”. The information kind of the buffers which are utilized in its enter and output arguments should be the pointers or arrays of components of kind char or unsigned char.

When utilizing the strcpy() operate, it’s necessary to allocate sufficient capability to the buffers that you simply’ll use, making an allowance for the size of the strings that this system will course of. If the dimensions of the vacation spot array is smaller than the dimensions of the supply string “s2”, an overflow or knowledge truncation will happen.

The string dealing with capabilities, together with strcpy(), are declared within the “string.h” header. To make use of them, you could embrace them in your code as follows:

The best way to Copy an Specific String with the Strcpy() Perform in C Language

On this instance, we are going to take a look at find out how to copy an express string utilizing the strcpy() operate.
To do that, we insert the “stdio.h” and “string.h” headers into an empty “.c” file. Then, we open a most important() operate that returns empty. We declare the array dest with 50 components of kind char in it, the place we retailer the copy.

After inserting the mandatory headers and declaring the vacation spot array, we name the strcpy() operate and move it as enter arguments in “s2”, the express string that we wish to copy. On this case, the basic phrase “Whats up World” and the vacation spot string dest in “s1”. Right here is the code for this instance:

#embrace <stdio.h>
#embrace <string.h>

void most important ()
{
 char dest[50]=“”;
 strcpy( dest, “Whats up Worldn);
 printf (“%s”, dest);
}

The next picture exhibits the compilation and execution of this code:

The best way to Copy an Implicitly Declared String with the Strcpy() Perform in C Language

On this instance, we are going to present you find out how to use the strcpy() operate to repeat an implicit string. To do that, we add the headers which are used within the earlier instance and declare the vacation spot array dest with 50 components of kind char inside the primary() operate and the pointer orig with the sentence “At this time is a good day to be taught programming” as a supply array.

We then name the strcpy() operate and move the vacation spot array dest as the primary enter argument and the pointer to the string orig because the second argument. We use the printf() operate to show the contents of the brand new string within the command console.

#embrace <stdio.h>
#embrace <string.h>

void most important ()
{
 char dest[50]=“”;
 char *orig;
 orig =nAt this time is a good day to be taught programmingn;
 strcpy( dest, orig);
 printf (“%s”, dest);
}

The next picture exhibits the compilation and execution of this code. As we are able to see, the printf() operate shows the contents of the dest string:

The brand new string will also be accessed by way of the pointer that’s returned as the results of strcpy() as proven within the following:

#embrace <stdio.h>
#embrace <string.h>

void most important ()
{
 char dest[50]=“”;
 char *orig;
 orig =nAt this time is a good day to be taught programmingn;
 printf (“%s”, strcpy( dest, orig));
}

The best way to Copy a Sure Variety of Characters from a String with the Strncpy() Variant in C Language

The strncpy() operate is a variant of strcpy() and is utilized in circumstances the place solely a specified variety of characters are to be copied. This operate takes three enter arguments. The primary two are the vacation spot and supply arrays, and the third is a size_t knowledge kind that specifies the variety of characters to repeat. The next code makes use of the strncpy() operate to repeat solely the primary 21 characters of the string:

#embrace <stdio.h>
#embrace <string.h>

void most important ()

{
 char dest[50]=“”;
 char *orig;
 orig =“At this time is a good day to be taught programmingn;
 printf (n%sn, strncpy( dest, orig, 21));
}

The next picture exhibits the results of operating this code. As we are able to see, strncpy() copies solely the primary 21 characters of the supply string:

Conclusion

On this Linuxhint article, we defined find out how to use the strcpy() operate to repeat the strings. We confirmed you a theoretical description of the syntax, the way it works, the enter and output arguments, and the information sorts which are utilized by every one.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments