strcpy() in C/C++

CC++Programming

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

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

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

Some key points of strcpy().

  • It copies the whole string to the destination string. It replaces the whole string instead of appending it.

  • It won’t change the source string.

Here is an example of strcpy() in C language,

Example

 Live Demo

#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
raja
Published on 08-Oct-2018 12:00:09
Advertisements