C Program to Copy String Using strcpy

cprogramming-Logo
C Programming Examples
C String Programs
C Loop Programs
C Function Programs
C Array Programs
C Integer Number Programs
C File I/O Programs

This C program is used to copy the string by using the library function strcpy().

Program:
#include<stdio.h>
#include<string.h>    

main()
{
    char source[] = "C Program";    
    char destination[50];    
    strcpy(destination, source);    
    printf("Source string: %s\n", source);    
    printf("Destination string: %s\n", destination);    
    return 0;
}
Output of program:
Source string: C Program
Destination string: C Program
Explanation:

This program is used to copy a character array's value from one character to another. So first of all, you have to include the stdio header file using the include preceding by # which tells that the header file needs to be process before compilation, hence named preprocessor directive. Also you have to include the string.h header file. The string.h header classifies one variable type, one macro, and various functions to manipulate arrays of characters within your program.

Then you have to define the main() function and it has been declared as void since no return type is associated with it. The inside the main() function, you have to take one character array name source[] and initialize with the string C Program. Then you have to again take another character array name destination[50] which has not been initialized. Now you have to implement the strcpy() function, which is the C library function strcpy(dest, src) copies the string pointed to by the src to dest.

The printf() function is then use to display the strings Source string: %s\n and operation performed after strcpy() is used in the form of output. The printf() is used twice first for source, second for displaying destination.


Scroll Back to Top