All articles
c

A program to copy one string into another string without using library function

Share this article

Share on LinkedIn Share on X (formerly Twitter)

C Programming Language 067

The following code shows how to write a program to copy one string into another string without using library function.

Program

#include <stdio.h>
 
void main()
{
    char str1[30], str2[30];
    int i;
 
    printf("Enter string: ");
    gets(str1);
 
    for (i = 0; str1[i] != '\0'; i++)
    {
        str2[i] = str1[i];
    }
    str2[i] = '\0';
 
    printf("str1 = %s \nstr2 = %s", str1, str2);
 
    printf("\n");
}

Result

Enter string: hello
str1 = hello
str2 = hello

Comments