Copy one string into another in C

by | Jan 7, 2022 | C

Home » C » Copy one string into another in C

Now we will see a program in which we use the strcpy function to copy one string into another.

srtcpy(s1, s2) according to this statement the second string ‘s2’ will be copied to the first string ‘s1’.

Table of Contents

Program

#include<stdio.h>
#include<string.h>
int main()
{
            char s1[20];
            char s2[]="Hello world";
           
            strcpy(s1,s2);
            printf("Source string =%s\n",s2);
            printf("copied string =%s\n",s1);
           
            return 0;
}

 

Output

Copy one string into another in C

Explanation

‘s1’ is declared with some size because we want the string ‘s2’ to get copied in ‘s1’. The array size given to ‘s1’ must be equal to or greater than the size of the string ‘s2’.

0 Comments

Submit a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Author