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’.

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’.

Leave a comment

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