Home >Backend Development >C++ >In C/C++, the strcpy() function is a function used to copy one string to another string

In C/C++, the strcpy() function is a function used to copy one string to another string

WBOY
WBOYforward
2023-09-09 08:49:02979browse

In C/C++, the strcpy() function is a function used to copy one string to another string

The function strcpy() is a standard library function. It is used to copy one string to another string. In C language, it is declared in the "string.h" header file, while in C language, it is declared in the cstring header file. It returns a pointer to the destination.

This is the syntax of strcpy() in C language,

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

Some key points of strcpy().

  • It copies the entire string into the target string. It replaces the entire string instead of appending it.

  • It does not change the source string.

The following is an example of strcpy() in C language:

Example

Online demonstration

#include <stdio.h>
#include<string.h>
int main() {
   char s1[] = "Hello world!";
   char s2[] = "Welcome";
   printf("String s1 before: %s\n", s1);
   strcpy(s1, s2);
   printf("String s1 after: %s\n", s1);
   printf("String s2 : %s", s2);
   return 0;
}

Output

String s1 before: Hello world!
String s1 after: Welcome
String s2 : Welcome

The above is the detailed content of In C/C++, the strcpy() function is a function used to copy one string to another string. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete