
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
strcpy in C and C++
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
#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
Advertisements