Copy one String into another String


This operation is used to copy all the characters of one string into another string.
In order to copy , we have to start from the first character of the given string which is at index 0 in the array and copy that character into another string. After copying first character we have to moves on to the second character and this process is repeated until NULL character is encountered in the given string.

Algorithm to Copy one String into another String


Step 1: Length = 0
Step 2: Repeat up to step 4 while S1 [Length] ≠ NULL
Step 3: S2 [Length] = S1 [Length]
Length = Length + 1
Step 4: S2[Length]= NULL
     

Program to Copy one String into another String


#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char *s1,*s2;
void scopy(char *s1,char *s2);
clrscr();
puts("Enter string:");
gets(s1);
scopy(s1,s2);
puts("Copied String is:");
puts(s2);
getch();
}
void scopy(char *s1,char *s2)
{
int length=0;
while(s1[length]!='\0')
{
s2[length]=s1[length];
length=length+1;
}
s2[length]='\0';
}

Download Projects


Download Programs