C Program to Copy One String into Other Without Using Library Function.

August 24, 2024 No Comments » Hits : 318





Program : C Program to Copy One String into Other Without Using Library Function.

#include<stdio.h>
#include<conio.h>
void main()
{
 char s1[100],s2[100];
 int i;
 //reading a string and finding its length
 printf("\nEnter the string :");
 gets(s1);
 i=0;
 while(s1[i]!='\0')
 {
  s2[i]=s1[i];
  i++;
 }
 //since the '\0' is not copied
 s2[i]='\0';
 printf("\nCopied String is %s ",s2);
 getch();
}

Output :

Enter the string : c4learn.com
Copied String is c4learn.com

Explanation :

i=0;
 while(s1[i]!='\0')
 {
  s2[i]=s1[i];
  i++;
 }
  1. Scan Entered String From Left to Right , Character by Character.
  2. In Each Iteration Copy One Character To New String Variable.
  3. As soon as Source or Original String Ends , Process of Coping Character Stops but we still haven’t Copied NULL Character into new String so , Append Null Character to New String.
s2[i]='\0';

Incoming search terms: