C Program to Copy String Without Using strcpy()

@codevarsity
 · 
2.1k views
 · 

In this sample C program, you'll learn to copy string without using the library function strcpy().

Code

#include <stdio.h>
int main()
{
    char s1[100], s2[100], i;

    printf("Enter string s1: ");
    scanf("%s",s1);

    for(i = 0; s1[i] != '\0'; ++i)
    {
        s2[i] = s1[i];
    }

    s2[i] = '\0';
    printf("String s2: %s", s2);

    return 0;
}

Output

Enter String s1: codevarsity 
String s2: codevarsity

Comments